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.withIsolationScope(async isolationScope => { isolationScope.setSDKProcessingMetadata({ request: { headers: headers ? utils.winterCGHeadersToDict(headers) : undefined, }, }); return core.continueTrace( { sentryTrace: _nullishCoalesce(_nullishCoalesce(sentryTraceHeader, () => ( _optionalChain([headers, 'optionalAccess', _ => _.get, 'call', _2 => _2('sentry-trace')]))), () => ( undefined)), baggage: _nullishCoalesce(baggageHeader, () => ( _optionalChain([headers, 'optionalAccess', _3 => _3.get, 'call', _4 => _4('baggage')]))), }, async () => { try { return await core.startSpan( { op: 'http.server', name: `${method} ${parameterizedRoute}`, attributes: { [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.nextjs', }, }, 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 { span && core.setHttpStatus(span, response.status); } catch (e) { // best effort - response may be undefined? } return response; }, ); } finally { if (!platformSupportsStreaming.platformSupportsStreaming() || process.env.NEXT_RUNTIME === 'edge') { // 1. Edge transport requires manual flushing // 2. Lambdas require manual flushing to prevent execution freeze before the event is sent await responseEnd.flushQueue(); } } }, ); }); }, }); } exports.wrapRouteHandlerWithSentry = wrapRouteHandlerWithSentry; //# sourceMappingURL=wrapRouteHandlerWithSentry.js.map