Skip to content

Commit 8579f1c

Browse files
authoredJul 21, 2022
feat(createError): support fatal and unhandled (#148)
1 parent 991d099 commit 8579f1c

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed
 

‎src/app.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,20 @@ export function createApp (options: AppOptions = {}): App {
5858
const event = createEvent(req, res)
5959
try {
6060
await handler(event)
61-
} catch (err) {
61+
} catch (_error: any) {
62+
const error = createError(_error)
63+
if (!isError(_error)) {
64+
error.unhandled = true
65+
}
66+
67+
if (error.unhandled || error.fatal) {
68+
console.error('[h3]', error.fatal ? '[fatal]' : '[unhandled]', error) // eslint-disable-line no-console
69+
}
70+
6271
if (options.onError) {
63-
await options.onError(err as Error, event)
72+
await options.onError(error, event)
6473
} else {
65-
if (!isError(err)) {
66-
console.error('[h3]', err) // eslint-disable-line no-console
67-
}
68-
await sendError(event, err as Error, !!options.debug)
74+
await sendError(event, error, !!options.debug)
6975
}
7076
}
7177
}

‎src/error.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import { MIMES } from './utils'
77
* @extends Error
88
* @property {Number} statusCode An Integer indicating the HTTP response status code.
99
* @property {String} statusMessage A String representing the HTTP status message
10+
* @property {String} fatal Indicates if the error is a fatal error.
11+
* @property {String} unhandled Indicates if the error was unhandled and auto captured.
1012
* @property {Any} data An extra data that will includes in the response.<br>
1113
* This can be used to pass additional information about the error.
1214
* @property {Boolean} internal Setting this property to <code>true</code> will mark error as an internal error
1315
*/
1416
export class H3Error extends Error {
1517
statusCode: number = 500
18+
fatal: boolean = false
19+
unhandled: boolean = false
1620
statusMessage: string = 'Internal Server Error'
1721
data?: any
1822
}
@@ -34,17 +38,11 @@ export function createError (input: string | Partial<H3Error>): H3Error {
3438

3539
const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : undefined)
3640

37-
if (input.statusCode) {
38-
err.statusCode = input.statusCode
39-
}
40-
41-
if (input.statusMessage) {
42-
err.statusMessage = input.statusMessage
43-
}
44-
45-
if (input.data) {
46-
err.data = input.data
47-
}
41+
if (input.statusCode) { err.statusCode = input.statusCode }
42+
if (input.statusMessage) { err.statusMessage = input.statusMessage }
43+
if (input.data) { err.data = input.data }
44+
if (input.fatal !== undefined) { err.fatal = input.fatal }
45+
if (input.unhandled !== undefined) { err.unhandled = input.unhandled }
4846

4947
return err
5048
}

0 commit comments

Comments
 (0)
Please sign in to comment.