Skip to content

Commit

Permalink
fix: only log unhandled and fatal errors (nitrojs#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored and WinterYukky committed Nov 1, 2022
1 parent b476546 commit c1b11d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/dev/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Worker } from 'worker_threads'
import { existsSync, promises as fsp } from 'fs'
import { debounce } from 'perfect-debounce'
import { App, createApp, eventHandler } from 'h3'
import { App, createApp, eventHandler, H3Error } from 'h3'
import httpProxy from 'http-proxy'
import { listen, Listener, ListenOptions } from 'listhen'
import { servePlaceholder } from 'serve-placeholder'
Expand Down Expand Up @@ -74,7 +74,7 @@ export function createDevServer (nitro: Nitro): NitroDevServer {
// Error handler
const errorHandler = nitro.options.devErrorHandler || defaultErrorHandler

let lastError: Error = null
let lastError: H3Error = null
let reloadPromise: Promise<void> = null

let currentWorker: NitroWorker = null
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ export default <NitroErrorHandler> function (error, event) {
}

// Console output
if (statusCode !== 404) {
console.error('[nitro] [request error]', error.message + '\n' + stack.map(l => ' ' + l.text).join(' \n'))
if (error.unhandled || error.fatal) {
const tags = [
'[nitro]',
'[request error]',
error.unhandled && '[unhandled]',
error.fatal && '[fatal]'
].filter(Boolean).join(' ')
console.error(tags, error.message + '\n' + stack.map(l => ' ' + l.text).join(' \n'))
}

event.res.statusCode = statusCode
Expand Down
4 changes: 2 additions & 2 deletions src/types/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Handler, CompatibilityEvent } from 'h3'
import type { Handler, CompatibilityEvent, H3Error } from 'h3'

export interface NitroEventHandler {
/**
Expand Down Expand Up @@ -44,4 +44,4 @@ export interface NitroDevEventHandler {
handler: Handler
}

export type NitroErrorHandler = (error: Error, event: CompatibilityEvent) => void | Promise<void>
export type NitroErrorHandler = (error: H3Error, event: CompatibilityEvent) => void | Promise<void>

0 comments on commit c1b11d6

Please sign in to comment.