fix(nuxt): fallback to static error page on server error (#6697)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
Anthony Fu 2022-08-17 17:14:00 +08:00 committed by GitHub
parent 95b3fb21c5
commit 69c281f384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 8 deletions

View File

@ -13,7 +13,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
statusCode, statusCode,
statusMessage, statusMessage,
message, 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>` ? `<pre>${stack.map(i => `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>`).join('\n')}</pre>`
: '', : '',
data: error.data data: error.data
@ -42,12 +42,23 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
return return
} }
// HTML response // HTML response (via SSR)
const url = withQuery('/__nuxt_error', errorObject) const isErrorPage = event.req.url?.startsWith('/__nuxt_error')
const html = await $fetch(url).catch((error) => { let html = !isErrorPage ? await $fetch(withQuery('/__nuxt_error', errorObject)).catch(() => null) : null
console.error('[nitro] Error while generating error response', error)
return errorObject.statusMessage // 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.setHeader('Content-Type', 'text/html;charset=UTF-8')
event.res.end(html) event.res.end(html)

View File

@ -181,7 +181,6 @@ describe('errors', () => {
const error = await res.json() const error = await res.json()
delete error.stack delete error.stack
expect(error).toMatchObject({ expect(error).toMatchObject({
description: process.env.NUXT_TEST_DEV ? expect.stringContaining('<pre>') : '',
message: 'This is a custom error', message: 'This is a custom error',
statusCode: 500, statusCode: 500,
statusMessage: 'Internal Server Error', statusMessage: 'Internal Server Error',