Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxt): don't override payload error if it is present
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 6, 2022
1 parent a931667 commit c809681
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
3 changes: 2 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/renderer.ts
Expand Up @@ -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 })
Expand Down
49 changes: 25 additions & 24 deletions test/basic.test.ts
Expand Up @@ -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/')
Expand Down Expand Up @@ -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')
})
})
15 changes: 13 additions & 2 deletions test/fixtures/basic/pages/error.vue
@@ -1,7 +1,18 @@
<template>
<div />
<div>
{{ state.attr }}
{{ data.something }}
</div>
</template>

<script setup>
throw new Error('This is a custom error')
const { data, error } = await useAsyncData(() => {
throw new Error('some error')
}, { server: true })
if (error.value) {
throw createError({ statusCode: 422, fatal: true, statusMessage: 'This is a custom error' })
}
const state = ref({ attr: 'Hello World' })
</script>

0 comments on commit c809681

Please sign in to comment.