wrapErrorGetInitialPropsWithSentry.js 3.1 KB

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