-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(nuxt): Split the Nitro error hook as Nuxt 5 stops importing h3 #24283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // eslint-disable-next-line import/no-extraneous-dependencies | ||
| import { H3Error } from 'h3'; | ||
| import { createCaptureErrorHook } from '../utils/captureError'; | ||
|
|
||
| /** | ||
| * Hook that can be added in a Nitro plugin. It captures an error and sends it to Sentry. | ||
| * | ||
| * For Nuxt v3/v4 (Nitro v2, h3 v1). | ||
| */ | ||
| export const sentryCaptureErrorHook = createCaptureErrorHook(error => | ||
| error instanceof H3Error ? error.statusCode : undefined, | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,12 @@ | ||
| import { captureException, getClient, getCurrentScope } from '@sentry/core'; | ||
| import { flushIfServerless } from '@sentry/core/server'; | ||
| // eslint-disable-next-line import/no-extraneous-dependencies | ||
| import { H3Error } from 'h3'; | ||
| import type { CapturedErrorContext } from 'nitropack/types'; | ||
| import { extractErrorContext } from '../utils'; | ||
| import { HTTPError } from 'nitro/h3'; | ||
| import { createCaptureErrorHook } from '../utils/captureError'; | ||
|
|
||
| /** | ||
| * Hook that can be added in a Nitro plugin. It captures an error and sends it to Sentry. | ||
| * Hook that can be added in a Nitro plugin. It captures an error and sends it to Sentry. | ||
| * | ||
| * For Nuxt v5+ (Nitro v3+, h3 v2). | ||
| */ | ||
| export async function sentryCaptureErrorHook(error: Error, errorContext: CapturedErrorContext): Promise<void> { | ||
| const sentryClient = getClient(); | ||
| const sentryClientOptions = sentryClient?.getOptions(); | ||
|
|
||
| if ( | ||
| sentryClientOptions && | ||
| 'enableNitroErrorHandler' in sentryClientOptions && | ||
| sentryClientOptions.enableNitroErrorHandler === false | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| // Do not handle 404 and 422 | ||
| if (error instanceof H3Error) { | ||
| // Do not report if status code is 3xx or 4xx | ||
| if (error.statusCode >= 300 && error.statusCode < 500) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if the cause (original error) was already captured by middleware instrumentation | ||
| // H3 wraps errors, so we need to check the cause property | ||
| 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(); | ||
| } | ||
| export const sentryCaptureErrorHook = createCaptureErrorHook(error => | ||
| // `isError` compares constructor names, so it also matches an error thrown by another copy of h3 | ||
| HTTPError.isError(error) ? error.status : undefined, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import type { NitroAppPlugin } from 'nitropack'; | ||
| import { sentryCaptureErrorHook } from '../hooks/captureErrorHook-legacy'; | ||
|
|
||
| /** | ||
| * Nitro plugin that reports server errors to Sentry for Nuxt v3/v4 (Nitro v2) | ||
| */ | ||
| export default (nitroApp => { | ||
| nitroApp.hooks.hook('error', sentryCaptureErrorHook); | ||
| }) satisfies NitroAppPlugin; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { NitroAppPlugin } from 'nitro/types'; | ||
| import { sentryCaptureErrorHook } from '../hooks/captureErrorHook'; | ||
|
|
||
| /** | ||
| * Nitro plugin that reports server errors to Sentry for Nuxt v5+ (Nitro v3+) | ||
| */ | ||
| export default (nitroApp => { | ||
| // @ts-expect-error Nitro v3 hands the `error` hook an `HTTPEvent`, Nitro v2 an `H3Event` | ||
| nitroApp.hooks.hook('error', sentryCaptureErrorHook); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l/q: Can't we do a function overload then? |
||
| }) satisfies NitroAppPlugin; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { captureException, getClient, getCurrentScope } from '@sentry/core'; | ||
| import { flushIfServerless } from '@sentry/core/server'; | ||
| import type { CapturedErrorContext } from 'nitropack/types'; | ||
| import { extractErrorContext } from '../utils'; | ||
|
|
||
| /** | ||
| * Reads the HTTP status off an error thrown by the server framework, or returns `undefined` for | ||
| * anything that is not one. h3 v1 (`H3Error.statusCode`) and h3 v2 (`HTTPError.status`) disagree on | ||
| * both the class and the field, so each Nitro variant passes in its own. | ||
| */ | ||
| export type GetHttpErrorStatus = (error: Error) => number | undefined; | ||
|
|
||
| /** | ||
| * Builds the hook a Nitro plugin registers on `error`. It captures the error and sends it to Sentry. | ||
| */ | ||
| export function createCaptureErrorHook( | ||
| getHttpErrorStatus: GetHttpErrorStatus, | ||
| ): (error: Error, errorContext: CapturedErrorContext) => Promise<void> { | ||
| return async function sentryCaptureErrorHook(error, errorContext): Promise<void> { | ||
| const sentryClient = getClient(); | ||
| const sentryClientOptions = sentryClient?.getOptions(); | ||
|
|
||
| if ( | ||
| sentryClientOptions && | ||
| 'enableNitroErrorHandler' in sentryClientOptions && | ||
| sentryClientOptions.enableNitroErrorHandler === false | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const status = getHttpErrorStatus(error); | ||
|
|
||
| if (status !== undefined) { | ||
| // Do not report if status code is 3xx or 4xx | ||
| if (status >= 300 && status < 500) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if the cause (original error) was already captured by middleware instrumentation | ||
| // H3 wraps errors, so we need to check the cause property | ||
| 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}`); | ||
| } | ||
|
Comment on lines
+51
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixUpdate the shared utility to handle both Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
||
| const structuredContext = extractErrorContext(errorContext); | ||
|
|
||
| captureException(error, { | ||
| captureContext: { contexts: { nuxt: structuredContext } }, | ||
| mechanism: { handled: false, type: 'auto.function.nuxt.nitro' }, | ||
| }); | ||
|
|
||
| await flushIfServerless(); | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: Why can't we simply move it to
nitro/h3entirely? Then we could keep everything in one function