|
| 1 | +import { Context, GLOBAL_OBJ, dropUndefinedKeys, flush, logger, vercelWaitUntil } from '@sentry/core'; |
| 2 | +import * as SentryNode from '@sentry/node'; |
| 3 | +import { H3Error } from 'h3'; |
| 4 | +import type { CapturedErrorContext } from 'nitropack'; |
| 5 | +import { defineNitroPlugin } from '#imports'; |
| 6 | + |
| 7 | +// Copy from SDK-internal error handler (nuxt/src/runtime/plugins/sentry.server.ts) |
| 8 | +export default defineNitroPlugin(nitroApp => { |
| 9 | + nitroApp.hooks.hook('error', async (error, errorContext) => { |
| 10 | + // Do not handle 404 and 422 |
| 11 | + if (error instanceof H3Error) { |
| 12 | + // Do not report if status code is 3xx or 4xx |
| 13 | + if (error.statusCode >= 300 && error.statusCode < 500) { |
| 14 | + return; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + const { method, path } = { |
| 19 | + method: errorContext.event?._method ? errorContext.event._method : '', |
| 20 | + path: errorContext.event?._path ? errorContext.event._path : null, |
| 21 | + }; |
| 22 | + |
| 23 | + if (path) { |
| 24 | + SentryNode.getCurrentScope().setTransactionName(`${method} ${path}`); |
| 25 | + } |
| 26 | + |
| 27 | + const structuredContext = extractErrorContext(errorContext); |
| 28 | + |
| 29 | + SentryNode.captureException(error, { |
| 30 | + captureContext: { contexts: { nuxt: structuredContext } }, |
| 31 | + mechanism: { handled: false }, |
| 32 | + }); |
| 33 | + |
| 34 | + await flushIfServerless(); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +function extractErrorContext(errorContext: CapturedErrorContext): Context { |
| 39 | + const structuredContext: Context = { |
| 40 | + method: undefined, |
| 41 | + path: undefined, |
| 42 | + tags: undefined, |
| 43 | + }; |
| 44 | + |
| 45 | + if (errorContext) { |
| 46 | + if (errorContext.event) { |
| 47 | + structuredContext.method = errorContext.event._method || undefined; |
| 48 | + structuredContext.path = errorContext.event._path || undefined; |
| 49 | + } |
| 50 | + |
| 51 | + if (Array.isArray(errorContext.tags)) { |
| 52 | + structuredContext.tags = errorContext.tags || undefined; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return dropUndefinedKeys(structuredContext); |
| 57 | +} |
| 58 | + |
| 59 | +async function flushIfServerless(): Promise<void> { |
| 60 | + const isServerless = |
| 61 | + !!process.env.FUNCTIONS_WORKER_RUNTIME || // Azure Functions |
| 62 | + !!process.env.LAMBDA_TASK_ROOT || // AWS Lambda |
| 63 | + !!process.env.VERCEL || |
| 64 | + !!process.env.NETLIFY; |
| 65 | + |
| 66 | + // @ts-expect-error This is not typed |
| 67 | + if (GLOBAL_OBJ[Symbol.for('@vercel/request-context')]) { |
| 68 | + vercelWaitUntil(flushWithTimeout()); |
| 69 | + } else if (isServerless) { |
| 70 | + await flushWithTimeout(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +async function flushWithTimeout(): Promise<void> { |
| 75 | + const sentryClient = SentryNode.getClient(); |
| 76 | + const isDebug = sentryClient ? sentryClient.getOptions().debug : false; |
| 77 | + |
| 78 | + try { |
| 79 | + isDebug && logger.log('Flushing events...'); |
| 80 | + await flush(2000); |
| 81 | + isDebug && logger.log('Done flushing events'); |
| 82 | + } catch (e) { |
| 83 | + isDebug && logger.log('Error while flushing events:\n', e); |
| 84 | + } |
| 85 | +} |
0 commit comments