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

Commit

Permalink
fix(nuxt): log fatal errors as well as unhandled ones (#6488)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Aug 10, 2022
1 parent 408feba commit 5aa7288
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/nuxt/src/core/runtime/nitro/error.ts
Original file line number Diff line number Diff line change
@@ -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> = {
Expand All @@ -17,16 +18,23 @@ 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
event.res.statusCode = errorObject.statusCode as any as number
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
Expand Down

0 comments on commit 5aa7288

Please sign in to comment.