@sentry/nextjs
Version:
Official Sentry SDK for Next.js
71 lines (58 loc) • 3.2 kB
JavaScript
import { _nullishCoalesce, _optionalChain } from '@sentry/utils';
import { addTracingExtensions, spanToTraceHeader, getDynamicSamplingContextFromSpan, getClient, getCurrentScope } from '@sentry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
import { isBuild } from './utils/isBuild.js';
import { withTracedServerSideDataFetcher, getTransactionFromRequest, withErrorInstrumentation } from './utils/wrapperUtils.js';
/**
* Create a wrapped version of the user's exported `getInitialProps` function in
* a custom error page ("_error.js").
*
* @param origErrorGetInitialProps The user's `getInitialProps` function
* @param parameterizedRoute The page's parameterized route
* @returns A wrapped version of the function
*/
function wrapErrorGetInitialPropsWithSentry(
origErrorGetInitialProps,
) {
return new Proxy(origErrorGetInitialProps, {
apply: async (wrappingTarget, thisArg, args) => {
if (isBuild()) {
return wrappingTarget.apply(thisArg, args);
}
addTracingExtensions();
const [context] = args;
const { req, res } = context;
const errorWrappedGetInitialProps = withErrorInstrumentation(wrappingTarget);
const options = _optionalChain([getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getOptions, 'call', _3 => _3()]);
// Generally we can assume that `req` and `res` are always defined on the server:
// https://nextjs.org/docs/api-reference/data-fetching/get-initial-props#context-object
// This does not seem to be the case in dev mode. Because we have no clean way of associating the the data fetcher
// span with each other when there are no req or res objects, we simply do not trace them at all here.
if (req && res && _optionalChain([options, 'optionalAccess', _4 => _4.instrumenter]) === 'sentry') {
const tracedGetInitialProps = withTracedServerSideDataFetcher(errorWrappedGetInitialProps, req, res, {
dataFetcherRouteName: '/_error',
requestedRouteName: context.pathname,
dataFetchingMethodName: 'getInitialProps',
});
const errorGetInitialProps
= await tracedGetInitialProps.apply(thisArg, args);
// eslint-disable-next-line deprecation/deprecation
const requestTransaction = _nullishCoalesce(getTransactionFromRequest(req), () => ( getCurrentScope().getTransaction()));
if (requestTransaction) {
errorGetInitialProps._sentryTraceData = spanToTraceHeader(requestTransaction);
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(requestTransaction);
errorGetInitialProps._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
}
return errorGetInitialProps;
} else {
return errorWrappedGetInitialProps.apply(thisArg, args);
}
},
});
}
/**
* @deprecated Use `wrapErrorGetInitialPropsWithSentry` instead.
*/
const withSentryServerSideErrorGetInitialProps = wrapErrorGetInitialPropsWithSentry;
export { withSentryServerSideErrorGetInitialProps, wrapErrorGetInitialPropsWithSentry };
//# sourceMappingURL=wrapErrorGetInitialPropsWithSentry.js.map