mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): fallback to static error page on server error (#6697)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
95b3fb21c5
commit
69c281f384
@ -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
|
||||
@ -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)
|
||||
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user