UNPKG

@sentry/nextjs

Version:
78 lines (67 loc) • 2.18 kB
var { _optionalChain } = require('@sentry/utils'); Object.defineProperty(exports, '__esModule', { value: true }); const core = require('@sentry/core'); const utils = require('@sentry/utils'); const responseEnd = require('./responseEnd.js'); /** * Wraps a function on the edge runtime with error and performance monitoring. */ function withEdgeWrapping( handler, options, ) { return async function ( ...args) { core.addTracingExtensions(); const req = args[0]; let sentryTrace; let baggage; if (req instanceof Request) { sentryTrace = req.headers.get('sentry-trace') || ''; baggage = req.headers.get('baggage'); } const transactionContext = core.continueTrace({ sentryTrace, baggage, }); return core.startSpan( { ...transactionContext, name: options.spanDescription, op: options.spanOp, origin: 'auto.function.nextjs.withEdgeWrapping', attributes: { [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route' }, metadata: { // eslint-disable-next-line deprecation/deprecation ...transactionContext.metadata, request: req instanceof Request ? utils.winterCGRequestToRequestData(req) : undefined, }, }, async span => { const handlerResult = await core.handleCallbackErrors( () => handler.apply(this, args), error => { core.captureException(error, { mechanism: { type: 'instrument', handled: false, data: { function: options.mechanismFunctionName, }, }, }); }, ); if (handlerResult instanceof Response) { _optionalChain([span, 'optionalAccess', _ => _.setHttpStatus, 'call', _2 => _2(handlerResult.status)]); } else { _optionalChain([span, 'optionalAccess', _3 => _3.setStatus, 'call', _4 => _4('ok')]); } return handlerResult; }, ).finally(() => responseEnd.flushQueue()); }; } exports.withEdgeWrapping = withEdgeWrapping; //# sourceMappingURL=edgeWrapperUtils.js.map