fix(nuxt): log fatal errors as well as unhandled ones (#6488)

This commit is contained in:
Daniel Roe 2022-08-10 16:47:23 +01:00 committed by GitHub
parent 408febada4
commit 5aa7288d97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,13 @@
import { withQuery } from 'ufo' import { withQuery } from 'ufo'
import type { NitroErrorHandler } from 'nitropack' import type { NitroErrorHandler } from 'nitropack'
import type { H3Error } from 'h3'
// @ts-ignore TODO // @ts-ignore TODO
import { normalizeError, isJsonRequest } from '#internal/nitro/utils' import { normalizeError, isJsonRequest } from '#internal/nitro/utils'
import { NuxtApp } from '#app' import { NuxtApp } from '#app'
export default <NitroErrorHandler> async function errorhandler (_error, event) { export default <NitroErrorHandler> async function errorhandler (error: H3Error, event) {
// Parse and normalize error // Parse and normalize error
const { stack, statusCode, statusMessage, message } = normalizeError(_error) const { stack, statusCode, statusMessage, message } = normalizeError(error)
// Create an error object // Create an error object
const errorObject: Exclude<NuxtApp['payload']['error'], Error> = { const errorObject: Exclude<NuxtApp['payload']['error'], Error> = {
@ -17,7 +18,7 @@ export default <NitroErrorHandler> async function errorhandler (_error, event) {
description: process.env.NODE_ENV === 'development' && statusCode !== 404 description: process.env.NODE_ENV === 'development' && 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 as any).data data: error.data
} }
// Set response code and message // Set response code and message
@ -25,8 +26,15 @@ export default <NitroErrorHandler> async function errorhandler (_error, event) {
event.res.statusMessage = errorObject.statusMessage event.res.statusMessage = errorObject.statusMessage
// Console output // Console output
if ((_error as any).unhandled) { if (error.unhandled || error.fatal) {
console.error('[nuxt] [unhandled request error]', errorObject.message + '\n' + stack.map(l => ' ' + l.text).join(' \n')) const tags = [
'[nuxt]',
'[request error]',
error.unhandled && '[unhandled]',
error.fatal && '[fatal]',
Number(errorObject.statusCode) !== 200 && `[${errorObject.statusCode}]`
].filter(Boolean).join(' ')
console.error(tags, errorObject.message + '\n' + stack.map(l => ' ' + l.text).join(' \n'))
} }
// JSON response // JSON response