UNPKG

@sentry/nextjs

Version:
91 lines (81 loc) • 3.46 kB
var { _nullishCoalesce, _optionalChain } = require('@sentry/utils'); Object.defineProperty(exports, '__esModule', { value: true }); const core = require('@sentry/core'); const utils = require('@sentry/utils'); const nextNavigationErrorUtils = require('./nextNavigationErrorUtils.js'); const platformSupportsStreaming = require('./utils/platformSupportsStreaming.js'); const responseEnd = require('./utils/responseEnd.js'); /** * Wraps a Next.js route handler with performance and error instrumentation. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any function wrapRouteHandlerWithSentry( routeHandler, context, ) { core.addTracingExtensions(); // eslint-disable-next-line deprecation/deprecation const { method, parameterizedRoute, baggageHeader, sentryTraceHeader, headers } = context; return new Proxy(routeHandler, { apply: (originalFunction, thisArg, args) => { return core.runWithAsyncContext(async () => { const { traceparentData, dynamicSamplingContext, propagationContext } = utils.tracingContextFromHeaders( _nullishCoalesce(_nullishCoalesce(sentryTraceHeader, () => ( _optionalChain([headers, 'optionalAccess', _ => _.get, 'call', _2 => _2('sentry-trace')]))), () => ( undefined)), _nullishCoalesce(baggageHeader, () => ( _optionalChain([headers, 'optionalAccess', _3 => _3.get, 'call', _4 => _4('baggage')]))), ); core.getCurrentScope().setPropagationContext(propagationContext); let result; try { result = await core.startSpan( { op: 'http.server', name: `${method} ${parameterizedRoute}`, status: 'ok', ...traceparentData, metadata: { request: { headers: headers ? utils.winterCGHeadersToDict(headers) : undefined, }, source: 'route', dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, }, }, async span => { const response = await core.handleCallbackErrors( () => originalFunction.apply(thisArg, args), error => { // Next.js throws errors when calling `redirect()`. We don't wanna report these. if (!nextNavigationErrorUtils.isRedirectNavigationError(error)) { core.captureException(error, { mechanism: { handled: false, }, }); } }, ); try { _optionalChain([span, 'optionalAccess', _5 => _5.setHttpStatus, 'call', _6 => _6(response.status)]); } catch (e) { // best effort } return response; }, ); } finally { if (!platformSupportsStreaming.platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') { // 1. Edge tranpsort requires manual flushing // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent await responseEnd.flushQueue(); } } return result; }); }, }); } exports.wrapRouteHandlerWithSentry = wrapRouteHandlerWithSentry; //# sourceMappingURL=wrapRouteHandlerWithSentry.js.map