wrapErrorGetInitialPropsWithSentry.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 isBuild = require('./utils/isBuild.js');
  9. const wrapperUtils = require('./utils/wrapperUtils.js');
  10. /**
  11. * Create a wrapped version of the user's exported `getInitialProps` function in
  12. * a custom error page ("_error.js").
  13. *
  14. * @param origErrorGetInitialProps The user's `getInitialProps` function
  15. * @param parameterizedRoute The page's parameterized route
  16. * @returns A wrapped version of the function
  17. */
  18. function wrapErrorGetInitialPropsWithSentry(
  19. origErrorGetInitialProps,
  20. ) {
  21. return new Proxy(origErrorGetInitialProps, {
  22. apply: async (wrappingTarget, thisArg, args) => {
  23. if (isBuild.isBuild()) {
  24. return wrappingTarget.apply(thisArg, args);
  25. }
  26. core.addTracingExtensions();
  27. const [context] = args;
  28. const { req, res } = context;
  29. const errorWrappedGetInitialProps = wrapperUtils.withErrorInstrumentation(wrappingTarget);
  30. const options = _optionalChain([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
  31. // Generally we can assume that `req` and `res` are always defined on the server:
  32. // https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
  33. // This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
  34. // span with each other when there are no req or res objects, we simply do not trace them at all here.
  35. if (req && res && _optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
  36. const tracedGetInitialProps = wrapperUtils.withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
  37. dataFetcherRouteName: '/_error',
  38. requestedRouteName: context.pathname,
  39. dataFetchingMethodName: 'getInitialProps',
  40. });
  41. const errorGetInitialProps
  42. = await tracedGetInitialProps.apply(thisArg, args);
  43. const activeSpan = core.getActiveSpan();
  44. const requestSpan = _nullishCoalesce(wrapperUtils.getSpanFromRequest(req), () => ( (activeSpan ? core.getRootSpan(activeSpan) : undefined)));
  45. if (requestSpan) {
  46. errorGetInitialProps._sentryTraceData = core.spanToTraceHeader(requestSpan);
  47. const dynamicSamplingContext = core.getDynamicSamplingContextFromSpan(requestSpan);
  48. errorGetInitialProps._sentryBaggage = utils.dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
  49. }
  50. return errorGetInitialProps;
  51. } else {
  52. return errorWrappedGetInitialProps.apply(thisArg, args);
  53. }
  54. },
  55. });
  56. }
  57. /**
  58. * @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead.
  59. */
  60. const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry;
  61. exports.withSentryServerSideErrorGetInitialProps = withSentryServerSideErrorGetInitialProps;
  62. exports.wrapErrorGetInitialPropsWithSentry = wrapErrorGetInitialPropsWithSentry;
  63. //# sourceMappingURL=wrapErrorGetInitialPropsWithSentry.js.map