UNPKG

@sentry/nextjs

Version:
96 lines (92 loc) 4.25 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const core = require('@sentry/core'); const responseEnd = require('../utils/responseEnd.js'); const tracingUtils = require('../utils/tracingUtils.js'); const attributes = require('@sentry/conventions/attributes'); function wrapApiHandlerWithSentry(apiHandler, parameterizedRoute) { return new Proxy(apiHandler, { apply: (wrappingTarget, thisArg, args) => { tracingUtils.dropNextjsRootContext(); return tracingUtils.escapeNextjsTracing(() => { const [req, res] = args; if (!req) { core.debug.log( `Wrapped API handler on route "${parameterizedRoute}" was not passed a request object. Will not instrument.` ); return wrappingTarget.apply(thisArg, args); } else if (!res) { core.debug.log( `Wrapped API handler on route "${parameterizedRoute}" was not passed a response object. Will not instrument.` ); return wrappingTarget.apply(thisArg, args); } if (req.__withSentry_applied__) { return wrappingTarget.apply(thisArg, args); } req.__withSentry_applied__ = true; return core.withIsolationScope((isolationScope) => { const continueTraceIfNoActiveSpan = core.getActiveSpan() ? (_opts, callback) => callback() : core.continueTrace; return continueTraceIfNoActiveSpan( { sentryTrace: req.headers && core.isString(req.headers["sentry-trace"]) ? req.headers["sentry-trace"] : void 0, baggage: req.headers?.baggage }, () => { const reqMethod = `${(req.method || "GET").toUpperCase()} `; const normalizedRequest = core.httpRequestToRequestData(req); isolationScope.setSDKProcessingMetadata({ normalizedRequest }); isolationScope.setTransactionName(`${reqMethod}${parameterizedRoute}`); const requestUrl = normalizedRequest.url || req.url; const urlObject = requestUrl ? core.parseStringToURLObject(requestUrl) : void 0; return core.startSpanManual( { name: `${reqMethod}${parameterizedRoute}`, op: "http.server", forceTransaction: true, attributes: { [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: "route", [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: "auto.http.nextjs", [attributes.URL_FULL]: urlObject && !core.isURLObjectRelative(urlObject) ? urlObject.href : void 0, [attributes.URL_PATH]: urlObject?.pathname, [attributes.HTTP_ROUTE]: parameterizedRoute } }, async (span) => { res.end = new Proxy(res.end, { apply(target, thisArg2, argArray) { core.setHttpStatus(span, res.statusCode); span.end(); responseEnd.waitUntil(responseEnd.flushSafelyWithTimeout()); return target.apply(thisArg2, argArray); } }); try { return await wrappingTarget.apply(thisArg, args); } catch (e) { const objectifiedErr = core.objectify(e); core.captureException(objectifiedErr, { mechanism: { type: "auto.http.nextjs.api_handler", handled: false, data: { wrapped_handler: wrappingTarget.name, function: "withSentry" } } }); core.setHttpStatus(span, 500); span.end(); await responseEnd.flushSafelyWithTimeout(); throw objectifiedErr; } } ); } ); }); }); } }); } exports.wrapApiHandlerWithSentry = wrapApiHandlerWithSentry; //# sourceMappingURL=wrapApiHandlerWithSentry.js.map