Repository navigation
feat(nuxt): Capture errors caught by <NuxtErrorBoundary> during SSR - #26815
Conversation
66b8cea to
ecc2714
Compare
|
@JPeer264 @chargome @nicohrubec I've implemented a fix for this by adding a server-only plugin (error-boundary.server.ts) that listens to vue:error during SSR and flushes caught errors on app:rendered using reportNuxtError. It skips reporting if nuxtApp.payload.error is present (so fatal page errors continue to be handled by Nitro without duplication) and filters out 3xx/4xx control-flow errors. Unit test coverage has been added. Please do review it |
|
@pranav514 can you please stop pinging random people from the team. we see that you opened a PR and we'll get to it. |
Sure I'll take care |
| function isIgnoredNuxtError(error: unknown): boolean { | ||
| if (!isNuxtError(error)) { | ||
| return false; | ||
| } | ||
| // oxlint-disable-next-line typescript/no-deprecated | ||
| const statusCode = error?.status || error?.statusCode; | ||
| return !!statusCode && statusCode >= 300 && statusCode < 500; | ||
| } |
There was a problem hiding this comment.
This function already exists in sentry.client.ts and can be reused. We can put this function into an utils file so it's shared and then re-use it from there.
| return !!statusCode && statusCode >= 300 && statusCode < 500; | ||
| } | ||
|
|
||
| export default defineNuxtPlugin(nuxtApp => { |
There was a problem hiding this comment.
This plugin is re-writing a lot of code that already exists. By reusing the things we already have, this can be condensed to just this:
export default defineNuxtPlugin(nuxtApp => {
const caught: Parameters<typeof reportNuxtError>[0][] = [];
nuxtApp.hook('vue:error', (error, instance, info) => {
if (!isIgnoredNuxtError(error)) caught.push({ error, instance, info });
});
// The Nitro `error` hook reports errors that end on the error page.
nuxtApp.hook('app:rendered', () => {
if (!nuxtApp.payload.error) caught.forEach(reportNuxtError);
});
});| addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector-legacy.server'), mode: 'server' }); | ||
| } | ||
|
|
||
| addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/error-boundary.server'), mode: 'server' }); |
There was a problem hiding this comment.
Could we add this plugin only when nuxt.options.experimental.ssrErrorBoundary is on?
Otherwise, the hooks just run without doing anything.
There was a problem hiding this comment.
I'd replace the unit tests with one E2E case in nuxt-5: a component that throws during SSR inside <NuxtErrorBoundary>, with a check that the page returns 200 and the error reaches Sentry. These mocks can't show that Nuxt actually calls vue:error.
|
@s1gr1d Addressed all feedback: Extracted isIgnoredNuxtError to shared runtime utils and reused it in both sentry.client.ts and error-boundary.server.ts. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit baa820c. Configure here.
|
|
||
| const error = await errorPromise; | ||
|
|
||
| expect(error.transaction).toEqual('/ssr-error-boundary'); |
There was a problem hiding this comment.
| expect(error.transaction).toEqual('/ssr-error-boundary'); | |
| expect(error.transaction).toEqual('GET /ssr-error-boundary'); |
could be that this is with GET as this is the server-side.
| } | ||
|
|
||
| // oxlint-disable-next-line typescript/no-unsafe-member-access | ||
| if (nuxt.options.experimental && (nuxt.options.experimental as Record<string, unknown>).ssrErrorBoundary) { |
There was a problem hiding this comment.
| if (nuxt.options.experimental && (nuxt.options.experimental as Record<string, unknown>).ssrErrorBoundary) { | |
| if ((nuxt.options.experimental as { ssrErrorBoundary?: boolean }).ssrErrorBoundary) { |
| experimental: { | ||
| ssrErrorBoundary: true, | ||
| }, |
There was a problem hiding this comment.
This is on by default, so we can remove this flag.
|
@s1gr1d Thanks for the review! Applied all suggested updates: Switched to the inline type check (nuxt.options.experimental as { ssrErrorBoundary?: boolean }).ssrErrorBoundary in module.ts. |
a6b5818 to
e3227e4
Compare
Nuxt 4.7 added SSR support to <NuxtErrorBoundary>, rendering its #error slot and returning HTTP 200 when a component errors during SSR. These errors were never forwarded to Sentry because Nitro's error hook only catches uncaught errors, and the client plugin does not receive vue:error during hydration. This registers a server-side Nuxt app plugin that listens to vue:error on the server and captures caught errors during app:rendered, unless the page errored fatally (nuxtApp.payload.error).
Avoid deferring via setTimeout so events are captured before Cloudflare or serverless request wrappers dispose the client. Calls flushIfServerless on app:rendered to ensure pending envelopes are dispatched.
- Share isIgnoredNuxtError in runtime utils for both client and server plugins - Condense error-boundary.server.ts to reuse reportNuxtError - Register plugin only when experimental.ssrErrorBoundary is enabled - Replace unit tests with an E2E test in nuxt-5 test application
- Use inline type for experimental.ssrErrorBoundary check - Remove redundant experimental flag from Nuxt 5 test app config - Expect HTTP method in server error transaction name - Assert error fallback visibility instead of matching sanitized error text
JPeer264
left a comment
There was a problem hiding this comment.
I'm waiting for the approval after the rebase, but after Sigrid's review it looks good
| @@ -0,0 +1,9 @@ | |||
| <script setup> | |||
There was a problem hiding this comment.
Just a note. The original issue was reported for Cloudflare, after the merge of #26756 this PR has a merge conflict, but after a rebase it would run also on Cloudflare, so we have this case covered too. No action item other than the rebase.
e3227e4 to
034ba29
Compare
s1gr1d
left a comment
There was a problem hiding this comment.
one small comment regarding the timings. And the optional nuxt-5 test does not run for external PRs in our CI, did you run that locally?
| }); | ||
| // The Nitro `error` hook reports errors that end on the error page. | ||
| nuxtApp.hook('app:rendered', () => { | ||
| if (!nuxtApp.payload.error) caught.forEach(reportNuxtError); |
There was a problem hiding this comment.
reportNuxtError wraps captureException in setTimeout. That delay is for client breadcrumbs, but now the server gets it too. On Cloudflare, the request wrapper flushes when the response ends, so the capture can come too late.
Could we remove the setTimeout from reportNuxtError and put it around the two calls in sentry.client.ts (in app:error, vue:error)? Then the server captures right away (captures in the next event loop, so that all breadcrumbs are recorded in time).
Kinda like this:
// utils.ts
captureException(error, {
captureContext: { contexts: { nuxt: metadata } },
mechanism: { handled: false, type: `auto.function.nuxt.${instance ? 'vue' : 'app'}-error` },
});
// in sentry.client.ts
setTimeout(() => reportNuxtError({ error }));
setTimeout(() => reportNuxtError({ error, instance, info }));…efer in client plugin Remove setTimeout from reportNuxtError so errors are captured immediately on the server before Cloudflare or serverless request wrappers flush and finish. Wrap calls in setTimeout within sentry.client.ts to preserve breadcrumb timing on the client.
|
@s1gr1d Makes complete sense! I've removed setTimeout from reportNuxtError so server-side errors (especially on Cloudflare) are captured synchronously before the response ends. The setTimeout delay is now placed directly in sentry.client.ts around app:error and vue:error to preserve client breadcrumbs. And yes, I built the package tarballs and ran the nuxt-5 E2E tests (tests/errors.server.test.ts) locally with Playwright, all 7 server test cases passed including the new SSR error boundary test. |
|
@JPeer264 Added the RUNTIME !== 'bun' guard and imported RUNTIME from ./runtime as suggested! |
This PR adds the external contributor to the CHANGELOG.md file, so that they are credited for their contribution. See #26815 Co-authored-by: JPeer264 <10677263+JPeer264@users.noreply.github.com>

What
Captures errors caught by
<NuxtErrorBoundary>during SSR in Nuxt 4.7+ and reports them to Sentry.Why
In Nuxt 4.7+,
<NuxtErrorBoundary>supports SSR and catches component errors to render its#errorslot with a 200 HTTP response. These errors never reached Sentry because Nitro'serrorhook only fires for unhandled server crashes and the client plugin does not receivevue:errorduring hydration. We buffer server-sidevue:errorevents and report them onapp:renderedviareportNuxtError, while ignoring intentional 3xx/4xx control-flow errors and fatal page crashes already reported by Nitro.Closes: #26795