GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — feat(nuxt): Capture errors caught by <NuxtErrorBoundary> during SSR by pranav514 · Pull Request #26815 · getsentry/sentry-javascript Conversation
Contributor
WhatCaptures errors caught by WhyIn Nuxt 4.7+, Closes: #26795
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
javascript-sdk-gitflow
Bot
requested review from
chargome,
nicohrubec and
s1gr1d
pranav514
force-pushed
the
feat/nuxt-ssr-error-boundary
branch
from
66b8cea to
ecc2714
Compare
Contributor
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@pranav514 can you please stop pinging random people from the team. we see that you opened a PR and we'll get to it. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sure I'll take care |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| 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.
The reason will be displayed to describe this comment to others. Learn more.
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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return !!statusCode && statusCode >= 300 && statusCode < 500; | ||
| } | ||
|
|
||
| export default defineNuxtPlugin(nuxtApp => { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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); }); });
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| 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.
The reason will be displayed to describe this comment to others. Learn more.
Could we add this plugin only when nuxt.options.experimental.ssrErrorBoundary is on?
Otherwise, the hooks just run without doing anything.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@s1gr1d Addressed all feedback: Extracted isIgnoredNuxtError to shared runtime utils and reused it in both sentry.client.ts and error-boundary.server.ts. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
|
||
| const error = await errorPromise; | ||
|
|
||
| expect(error.transaction).toEqual('/ssr-error-boundary'); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| // 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.
The reason will be displayed to describe this comment to others. Learn more.
| if (nuxt.options.experimental && (nuxt.options.experimental as Record<string, unknown>).ssrErrorBoundary) { | |
| if ((nuxt.options.experimental as { ssrErrorBoundary?: boolean }).ssrErrorBoundary) { |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| experimental: { | ||
| ssrErrorBoundary: true, | ||
| }, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is on by default, so we can remove this flag.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm waiting for the approval after the rebase, but after Sigrid's review it looks good
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| @@ -0,0 +1,9 @@ | |||
| <script setup> | |||
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
e3227e4 to
034ba29
Compare
|
@JPeer264 Rebased onto latest develop and resolved the conflict in packages/nuxt/src/module.ts. The branch now includes #26756 so the E2E test will also run against Cloudflare in CI. Ready for review! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM from my side. Waiting for @s1gr1d's approval before we merge
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| }); | ||
| // 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.
The reason will be displayed to describe this comment to others. Learn more.
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 }));
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@JPeer264 Added the RUNTIME !== 'bun' guard and imported RUNTIME from ./runtime as suggested! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
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 <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Nuxt: report errors caught by <NuxtErrorBoundary> during SSR
There was an error while loading. Please reload this page.