Skip to content

Commit

Permalink
feat: custom error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 12, 2020
1 parent edf40b9 commit ad3515f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export function createApp (options: AppOptions = {}): App {

// @ts-ignore
const app: Partial<App> = function (req: IncomingMessage, res: ServerResponse) {
return _handle(req, res)
.catch((err: Error | any) => {
// @ts-ignore
err.internal = true
sendError(res, err, !!options.debug)
})
return _handle(req, res).catch((error: Error) => {
if (options.onError) {
return options.onError(error, req, res)
}
// @ts-ignore
error.internal = true
return sendError(res, error, !!options.debug)
})
}

app.stack = stack
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export interface App {

export interface AppOptions {
debug?: boolean
onError?: (error: Error, req: IncomingMessage, res: ServerResponse) => any
}

0 comments on commit ad3515f

Please sign in to comment.