Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup>
if (import.meta.server) {
throw new Error('SSR Error inside NuxtErrorBoundary');
}
</script>

<template>
<div>Normal SSR Content</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script setup>
import SSRErrorComponent from '../components/SSRErrorComponent.vue';
</script>

<template>
<div>
<NuxtErrorBoundary>
<SSRErrorComponent />
<template #error="{ error }">
<p id="error-fallback">Caught SSR Error: {{ error.message }}</p>
</template>
</NuxtErrorBoundary>
</div>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,28 @@ IMPORT_SURFACES.forEach(({ name, apiPrefix }) => {
});
});
});

test.describe('SSR error boundary', () => {
test('captures error thrown in NuxtErrorBoundary during SSR with 200 response', async ({ page }) => {
const errorPromise = waitForError('nuxt-5', async errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'SSR Error inside NuxtErrorBoundary';
});

const response = await page.goto('/ssr-error-boundary');
expect(response?.status()).toBe(200);

const error = await errorPromise;

expect(error.transaction).toEqual('GET /ssr-error-boundary');
expect(error.exception.values[0]).toMatchObject({
type: 'Error',
value: 'SSR Error inside NuxtErrorBoundary',
mechanism: {
handled: false,
type: 'auto.function.nuxt.vue-error',
},
});

await expect(page.locator('#error-fallback')).toBeVisible();
});
});
4 changes: 4 additions & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ export default defineNuxtModule<ModuleOptions>({
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector-legacy.server'), mode: 'server' });
}

if ((nuxt.options.experimental as { ssrErrorBoundary?: boolean }).ssrErrorBoundary) {
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/error-boundary.server'), mode: 'server' });
}

if (clientConfigFile || serverConfigFile) {
// The bundler plugins do not get `sourcemaps.delete`.
warnOnConflictingSourceMapOptions(moduleOptions.sourcemaps);
Expand Down
14 changes: 14 additions & 0 deletions packages/nuxt/src/runtime/plugins/error-boundary.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineNuxtPlugin } from 'nuxt/app';
import { reportNuxtError } from '../utils';
import { isIgnoredNuxtError } from '../utils/isIgnoredNuxtError';

export default defineNuxtPlugin(nuxtApp => {

Copy link
Copy Markdown
Member

Choose a reason for 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);
  });
});

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);
});
});
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
13 changes: 2 additions & 11 deletions packages/nuxt/src/runtime/plugins/sentry.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
setRouteProvider,
vueIntegration,
} from '@sentry/vue';
import { defineNuxtPlugin, isNuxtError } from 'nuxt/app';
import { defineNuxtPlugin } from 'nuxt/app';
import type { GlobalObjWithIntegrationOptions } from '../../client/vueIntegration';
import { reportNuxtError } from '../utils';
import { isIgnoredNuxtError } from '../utils/isIgnoredNuxtError';

// --- Types are copied from @sentry/vue (so it does not need to be exported) ---
// The following type is an intersection of the Route type from VueRouter v2, v3, and v4.
Expand Down Expand Up @@ -38,16 +39,6 @@ interface VueRouter {

type VueRouteProviderRouter = ReturnType<Parameters<typeof _INTERNAL_createVueRouteProvider>[0]>;

// 3xx/4xx Nuxt errors are expected control flow (redirects, not-found pages), not bugs. The server skips them too.
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;
}

// Tree-shakable guard to remove all code related to tracing
declare const __SENTRY_TRACING__: boolean;

Expand Down
11 changes: 11 additions & 0 deletions packages/nuxt/src/runtime/utils/isIgnoredNuxtError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isNuxtError } from 'nuxt/app';

// 3xx/4xx Nuxt errors are expected control flow (redirects, not-found pages), not bugs. The server skips them too.
export 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;
}