Skip to content

Commit

Permalink
fix: make console error for thrown unkown errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 31, 2022
1 parent 2f4859c commit 1552219
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type http from 'http'
import { withoutTrailingSlash } from 'ufo'
import { defineLazyEventHandler, toEventHandler, createEvent, isEventHandler, defineEventHandler } from './event'
import { createError, sendError } from './error'
import { createError, sendError, isError } from './error'
import { send, sendStream, isStream, MIMES } from './utils'
import type { Handler, LazyHandler, Middleware } from './types'
import type { EventHandler, CompatibilityEvent, CompatibilityEventHandler, LazyEventHandler } from './event'
Expand Down Expand Up @@ -62,6 +62,9 @@ export function createApp (options: AppOptions = {}): App {
if (options.onError) {
await options.onError(err as Error, event)
}
if (!isError(err)) {
console.error('[h3]', err) // eslint-disable-line no-console
}
await sendError(event, err as Error, !!options.debug)
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ export function createError (input: Partial<H3Error>): H3Error {
* In the debug mode the stack trace of errors will be return in response.
*/
export function sendError (event: CompatibilityEvent, error: Error | H3Error, debug?: boolean) {
let h3Error: H3Error
if (error instanceof H3Error) {
h3Error = error
} else {
console.error('[h3]', error) // eslint-disable-line no-console
h3Error = createError(error)
}
const h3Error = isError(error) ? error : createError(error)

if (event.res.writableEnded) {
return
Expand All @@ -85,3 +79,7 @@ export function sendError (event: CompatibilityEvent, error: Error | H3Error, de
event.res.setHeader('Content-Type', MIMES.json)
event.res.end(JSON.stringify(responseBody, null, 2))
}

export function isError (input: any): input is H3Error {
return input instanceof H3Error
}

0 comments on commit 1552219

Please sign in to comment.