@sentry/nextjs
Version:
Official Sentry SDK for Next.js
60 lines (57 loc) • 2.31 kB
JavaScript
import { getIsolationScope, winterCGHeadersToDict, handleCallbackErrors, getActiveSpan, SPAN_STATUS_ERROR, SPAN_STATUS_OK, captureException } from '@sentry/core';
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils.js';
import { waitUntil, flushSafelyWithTimeout } from './utils/responseEnd.js';
function wrapGenerationFunctionWithSentry(generationFunction, context) {
return new Proxy(generationFunction, {
apply: (originalFunction, thisArg, args) => {
const isolationScope = getIsolationScope();
let headers = void 0;
try {
headers = context.requestAsyncStorage?.getStore()?.headers;
} catch {
}
const headersDict = headers ? winterCGHeadersToDict(headers) : void 0;
isolationScope.setSDKProcessingMetadata({
normalizedRequest: {
headers: headersDict
}
});
return handleCallbackErrors(
() => originalFunction.apply(thisArg, args),
(error) => {
const span = getActiveSpan();
const { componentRoute, componentType, generationFunctionIdentifier } = context;
let shouldCapture = true;
isolationScope.setTransactionName(`${componentType}.${generationFunctionIdentifier} (${componentRoute})`);
if (span) {
if (isNotFoundNavigationError(error)) {
shouldCapture = false;
span.setStatus({ code: SPAN_STATUS_ERROR, message: "not_found" });
} else if (isRedirectNavigationError(error)) {
shouldCapture = false;
span.setStatus({ code: SPAN_STATUS_OK });
} else {
span.setStatus({ code: SPAN_STATUS_ERROR, message: "internal_error" });
}
}
if (shouldCapture) {
captureException(error, {
mechanism: {
handled: false,
type: "auto.function.nextjs.generation_function",
data: {
function: generationFunctionIdentifier
}
}
});
}
},
() => {
waitUntil(flushSafelyWithTimeout());
}
);
}
});
}
export { wrapGenerationFunctionWithSentry };
//# sourceMappingURL=wrapGenerationFunctionWithSentry.js.map