-
-
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
Open
s1gr1d
wants to merge
2
commits into
develop
Choose a base branch
from
t3code/9c33bc1e
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/nuxt/src/runtime/hooks/captureErrorHook-legacy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ); |
9 changes: 9 additions & 0 deletions
9
packages/nuxt/src/runtime/plugins/capture-error-legacy.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }) satisfies NitroAppPlugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}`); | ||
| } | ||
|
|
||
| const structuredContext = extractErrorContext(errorContext); | ||
|
|
||
| captureException(error, { | ||
| captureContext: { contexts: { nuxt: structuredContext } }, | ||
| mechanism: { handled: false, type: 'auto.function.nuxt.nitro' }, | ||
| }); | ||
|
|
||
| await flushIfServerless(); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Bug: The
createCaptureErrorHookutility accessesevent._methodandevent._path, which are internal toh3 v1. These fields do not exist inh3 v2(used by Nitro v3/Nuxt 5+).Severity: MEDIUM
Suggested Fix
Update the shared utility to handle both
h3 v1andh3 v2event shapes. Check for the standardevent.pathbefore falling back toevent._path, and useevent.req.methodorevent.methodto get the request method, ensuring compatibility with both Nitro v2 and v3.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.