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

Commit

Permalink
fix(nuxt): fallback to static error page on server error (#6697)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
antfu and pi0 committed Aug 17, 2022
1 parent 95b3fb2 commit 69c281f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
25 changes: 18 additions & 7 deletions packages/nuxt/src/core/runtime/nitro/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
statusCode,
statusMessage,
message,
description: process.env.NODE_ENV === 'development' && statusCode !== 404
stack: process.dev && statusCode !== 404
? `<pre>${stack.map(i => `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>`).join('\n')}</pre>`
: '',
data: error.data
Expand Down Expand Up @@ -42,12 +42,23 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
return
}

// HTML response
const url = withQuery('/__nuxt_error', errorObject)
const html = await $fetch(url).catch((error) => {
console.error('[nitro] Error while generating error response', error)
return errorObject.statusMessage
})
// HTML response (via SSR)
const isErrorPage = event.req.url?.startsWith('/__nuxt_error')
let html = !isErrorPage ? await $fetch(withQuery('/__nuxt_error', errorObject)).catch(() => null) : null

// Fallback to static rendered error page
if (!html) {
const { template } = process.dev
// @ts-ignore
? await import('@nuxt/ui-templates/templates/error-dev.mjs')
// @ts-ignore
: await import('@nuxt/ui-templates/templates/error-500.mjs')
if (process.dev) {
// TODO: Support `message` in template
(errorObject as any).description = errorObject.message
}
html = template(errorObject)
}

event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
event.res.end(html)
Expand Down
1 change: 0 additions & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ describe('errors', () => {
const error = await res.json()
delete error.stack
expect(error).toMatchObject({
description: process.env.NUXT_TEST_DEV ? expect.stringContaining('<pre>') : '',
message: 'This is a custom error',
statusCode: 500,
statusMessage: 'Internal Server Error',
Expand Down

0 comments on commit 69c281f

Please sign in to comment.