wrapGenerationFunctionWithSentry.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 commonObjectTracing = require('./utils/commonObjectTracing.js');
  10. /**
  11. * Wraps a generation function (e.g. generateMetadata) with Sentry error and performance instrumentation.
  12. */
  13. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  14. function wrapGenerationFunctionWithSentry(
  15. generationFunction,
  16. context,
  17. ) {
  18. core.addTracingExtensions();
  19. const { requestAsyncStorage, componentRoute, componentType, generationFunctionIdentifier } = context;
  20. return new Proxy(generationFunction, {
  21. apply: (originalFunction, thisArg, args) => {
  22. let headers = undefined;
  23. // We try-catch here just in case anything goes wrong with the async storage here goes wrong since it is Next.js internal API
  24. try {
  25. headers = _optionalChain([requestAsyncStorage, 'optionalAccess', _ => _.getStore, 'call', _2 => _2(), 'optionalAccess', _3 => _3.headers]);
  26. } catch (e) {
  27. /** empty */
  28. }
  29. let data = undefined;
  30. if (_optionalChain([core.getClient, 'call', _4 => _4(), 'optionalAccess', _5 => _5.getOptions, 'call', _6 => _6(), 'access', _7 => _7.sendDefaultPii])) {
  31. const props = args[0];
  32. const params = props && typeof props === 'object' && 'params' in props ? props.params : undefined;
  33. const searchParams =
  34. props && typeof props === 'object' && 'searchParams' in props ? props.searchParams : undefined;
  35. data = { params, searchParams };
  36. }
  37. return core.withIsolationScope(isolationScope => {
  38. isolationScope.setSDKProcessingMetadata({
  39. request: {
  40. headers: headers ? utils.winterCGHeadersToDict(headers) : undefined,
  41. },
  42. });
  43. isolationScope.setExtra('route_data', data);
  44. const incomingPropagationContext = utils.propagationContextFromHeaders(
  45. _nullishCoalesce(_optionalChain([headers, 'optionalAccess', _8 => _8.get, 'call', _9 => _9('sentry-trace')]), () => ( undefined)),
  46. _optionalChain([headers, 'optionalAccess', _10 => _10.get, 'call', _11 => _11('baggage')]),
  47. );
  48. const propagationContext = commonObjectTracing.commonObjectToPropagationContext(headers, incomingPropagationContext);
  49. isolationScope.setPropagationContext(propagationContext);
  50. core.getCurrentScope().setPropagationContext(propagationContext);
  51. return core.startSpanManual(
  52. {
  53. op: 'function.nextjs',
  54. name: `${componentType}.${generationFunctionIdentifier} (${componentRoute})`,
  55. data,
  56. attributes: {
  57. [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
  58. [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs',
  59. },
  60. },
  61. span => {
  62. return core.handleCallbackErrors(
  63. () => originalFunction.apply(thisArg, args),
  64. err => {
  65. if (nextNavigationErrorUtils.isNotFoundNavigationError(err)) {
  66. // We don't want to report "not-found"s
  67. _optionalChain([span, 'optionalAccess', _12 => _12.setStatus, 'call', _13 => _13('not_found')]);
  68. } else if (nextNavigationErrorUtils.isRedirectNavigationError(err)) {
  69. // We don't want to report redirects
  70. _optionalChain([span, 'optionalAccess', _14 => _14.setStatus, 'call', _15 => _15('ok')]);
  71. } else {
  72. _optionalChain([span, 'optionalAccess', _16 => _16.setStatus, 'call', _17 => _17('internal_error')]);
  73. core.captureException(err, {
  74. mechanism: {
  75. handled: false,
  76. },
  77. });
  78. }
  79. },
  80. () => {
  81. _optionalChain([span, 'optionalAccess', _18 => _18.end, 'call', _19 => _19()]);
  82. },
  83. );
  84. },
  85. );
  86. });
  87. },
  88. });
  89. }
  90. exports.wrapGenerationFunctionWithSentry = wrapGenerationFunctionWithSentry;
  91. //# sourceMappingURL=wrapGenerationFunctionWithSentry.js.map