diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index d26d6f48e3..ce6c682ff1 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -124,7 +124,8 @@ export default defineRenderHandler(async (event) => { const renderer = (process.env.NUXT_NO_SSR || ssrContext.noSSR) ? await getSPARenderer() : await getSSRRenderer() const _rendered = await renderer.renderToString(ssrContext).catch((err) => { if (!ssrError) { - throw err + // Use explicitly thrown error in preference to subsequent rendering errors + throw ssrContext.payload?.error || err } }) await ssrContext.nuxt?.hooks.callHook('app:rendered', { ssrContext }) diff --git a/test/basic.test.ts b/test/basic.test.ts index d15b091937..e236183e0b 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -202,30 +202,6 @@ describe('navigate external', () => { }) }) -describe('errors', () => { - it('should render a JSON error page', async () => { - const res = await fetch('/error', { - headers: { - accept: 'application/json' - } - }) - expect(res.status).toBe(500) - const error = await res.json() - delete error.stack - expect(error).toMatchObject({ - message: 'This is a custom error', - statusCode: 500, - statusMessage: 'Internal Server Error', - url: '/error' - }) - }) - - it('should render a HTML error page', async () => { - const res = await fetch('/error') - expect(await res.text()).toContain('This is a custom error') - }) -}) - describe('middlewares', () => { it('should redirect to index with global middleware', async () => { const html = await $fetch('/redirect/') @@ -592,3 +568,28 @@ describe('useAsyncData', () => { await expectNoClientErrors('/useAsyncData/promise-all') }) }) + +// TODO: Move back up after https://github.com/vuejs/core/issues/6110 is resolved +describe('errors', () => { + it('should render a JSON error page', async () => { + const res = await fetch('/error', { + headers: { + accept: 'application/json' + } + }) + expect(res.status).toBe(422) + const error = await res.json() + delete error.stack + expect(error).toMatchObject({ + message: 'This is a custom error', + statusCode: 422, + statusMessage: 'This is a custom error', + url: '/error' + }) + }) + + it('should render a HTML error page', async () => { + const res = await fetch('/error') + expect(await res.text()).toContain('This is a custom error') + }) +}) diff --git a/test/fixtures/basic/pages/error.vue b/test/fixtures/basic/pages/error.vue index 6956f59001..a8fe5801bd 100644 --- a/test/fixtures/basic/pages/error.vue +++ b/test/fixtures/basic/pages/error.vue @@ -1,7 +1,18 @@