@sentry/nextjs
Version:
Official Sentry SDK for Next.js
46 lines (43 loc) • 1.63 kB
JavaScript
import { withIsolationScope, getCurrentScope, winterCGRequestToRequestData, getActiveSpan, getRootSpan, setCapturedScopesOnSpan, handleCallbackErrors, captureException } from '@sentry/core';
import { waitUntil, flushSafelyWithTimeout } from './utils/responseEnd.js';
function wrapMiddlewareWithSentry(middleware) {
return new Proxy(middleware, {
apply: async (wrappingTarget, thisArg, args) => {
return withIsolationScope((isolationScope) => {
const req = args[0];
const currentScope = getCurrentScope();
if (req instanceof Request) {
isolationScope.setSDKProcessingMetadata({
normalizedRequest: winterCGRequestToRequestData(req)
});
currentScope.setTransactionName(`middleware ${req.method}`);
} else {
currentScope.setTransactionName("middleware");
}
const activeSpan = getActiveSpan();
if (activeSpan) {
const rootSpan = getRootSpan(activeSpan);
if (rootSpan) {
setCapturedScopesOnSpan(rootSpan, currentScope, isolationScope);
}
}
return handleCallbackErrors(
() => wrappingTarget.apply(thisArg, args),
(error) => {
captureException(error, {
mechanism: {
type: "auto.function.nextjs.wrap_middleware",
handled: false
}
});
},
() => {
waitUntil(flushSafelyWithTimeout());
}
);
});
}
});
}
export { wrapMiddlewareWithSentry };
//# sourceMappingURL=wrapMiddlewareWithSentry.js.map