wrapRouteHandlerWithSentry.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var {
  2. _nullishCoalesce,
  3. _optionalChain
  4. } = require('@sentry/utils');
  5. Object.defineProperty(exports, '__esModule', { value: true });
  6. const core = require('@sentry/core');
  7. const utils = require('@sentry/utils');
  8. const nextNavigationErrorUtils = require('./nextNavigationErrorUtils.js');
  9. const platformSupportsStreaming = require('./utils/platformSupportsStreaming.js');
  10. const responseEnd = require('./utils/responseEnd.js');
  11. /**
  12. * Wraps a Next.js route handler with performance and error instrumentation.
  13. */
  14. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  15. function wrapRouteHandlerWithSentry(
  16. routeHandler,
  17. context,
  18. ) {
  19. core.addTracingExtensions();
  20. // eslint-disable-next-line deprecation/deprecation
  21. const { method, parameterizedRoute, baggageHeader, sentryTraceHeader, headers } = context;
  22. return new Proxy(routeHandler, {
  23. apply: (originalFunction, thisArg, args) => {
  24. return core.withIsolationScope(async isolationScope => {
  25. isolationScope.setSDKProcessingMetadata({
  26. request: {
  27. headers: headers ? utils.winterCGHeadersToDict(headers) : undefined,
  28. },
  29. });
  30. return core.continueTrace(
  31. {
  32. sentryTrace: _nullishCoalesce(_nullishCoalesce(sentryTraceHeader, () => ( _optionalChain([headers, 'optionalAccess', _ => _.get, 'call', _2 => _2('sentry-trace')]))), () => ( undefined)),
  33. baggage: _nullishCoalesce(baggageHeader, () => ( _optionalChain([headers, 'optionalAccess', _3 => _3.get, 'call', _4 => _4('baggage')]))),
  34. },
  35. async () => {
  36. try {
  37. return await core.startSpan(
  38. {
  39. op: 'http.server',
  40. name: `${method} ${parameterizedRoute}`,
  41. attributes: {
  42. [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
  43. [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
  44. },
  45. },
  46. async span => {
  47. const response = await core.handleCallbackErrors(
  48. () => originalFunction.apply(thisArg, args),
  49. error => {
  50. // Next.js throws errors when calling `redirect()`. We don't wanna report these.
  51. if (!nextNavigationErrorUtils.isRedirectNavigationError(error)) {
  52. core.captureException(error, {
  53. mechanism: {
  54. handled: false,
  55. },
  56. });
  57. }
  58. },
  59. );
  60. try {
  61. span && core.setHttpStatus(span, response.status);
  62. } catch (e) {
  63. // best effort - response may be undefined?
  64. }
  65. return response;
  66. },
  67. );
  68. } finally {
  69. if (!platformSupportsStreaming.platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') {
  70. // 1. Edge transport requires manual flushing
  71. // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent
  72. await responseEnd.flushQueue();
  73. }
  74. }
  75. },
  76. );
  77. });
  78. },
  79. });
  80. }
  81. exports.wrapRouteHandlerWithSentry = wrapRouteHandlerWithSentry;
  82. //# sourceMappingURL=wrapRouteHandlerWithSentry.js.map