import { captureException, flushIfServerless, getClient, getCurrentScope } from "@sentry/core"; import { H3Error } from "h3"; import { extractErrorContext } from "../utils.js"; export async function sentryCaptureErrorHook(error, errorContext) { const sentryClient = getClient(); const sentryClientOptions = sentryClient?.getOptions(); if (sentryClientOptions && "enableNitroErrorHandler" in sentryClientOptions && sentryClientOptions.enableNitroErrorHandler === false) { return; } if (error instanceof H3Error) { if (error.statusCode >= 300 && error.statusCode < 500) { return; } if ("cause" in error && typeof error.cause === "object" && error.cause !== null && "__sentry_captured__" in error.cause) { return; } } const { method, path } = { method: errorContext.event?._method ? errorContext.event._method : "", path: errorContext.event?._path ? errorContext.event._path : null }; if (path) { getCurrentScope().setTransactionName(`${method} ${path}`); } const structuredContext = extractErrorContext(errorContext); captureException(error, { captureContext: { contexts: { nuxt: structuredContext } }, mechanism: { handled: false, type: "auto.function.nuxt.nitro" } }); await flushIfServerless(); }