mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
fix(nuxt): log fatal errors as well as unhandled ones (#6488)
This commit is contained in:
parent
408febada4
commit
5aa7288d97
@ -1,12 +1,13 @@
|
||||
import { withQuery } from 'ufo'
|
||||
import type { NitroErrorHandler } from 'nitropack'
|
||||
import type { H3Error } from 'h3'
|
||||
// @ts-ignore TODO
|
||||
import { normalizeError, isJsonRequest } from '#internal/nitro/utils'
|
||||
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
|
||||
const { stack, statusCode, statusMessage, message } = normalizeError(_error)
|
||||
const { stack, statusCode, statusMessage, message } = normalizeError(error)
|
||||
|
||||
// Create an error object
|
||||
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
|
||||
? `<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
|
||||
@ -25,8 +26,15 @@ export default <NitroErrorHandler> async function errorhandler (_error, event) {
|
||||
event.res.statusMessage = errorObject.statusMessage
|
||||
|
||||
// Console output
|
||||
if ((_error as any).unhandled) {
|
||||
console.error('[nuxt] [unhandled request error]', errorObject.message + '\n' + stack.map(l => ' ' + l.text).join(' \n'))
|
||||
if (error.unhandled || error.fatal) {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user