@@ -13,9 +13,8 @@ import { MIMES } from './utils'
1313 */
1414export class H3Error extends Error {
1515 statusCode : number = 500
16- statusMessage : string = 'Internal Error '
16+ statusMessage : string = 'H3Error '
1717 data ?: any
18- internal : boolean = false
1918}
2019
2120/**
@@ -30,20 +29,19 @@ export function createError (input: Partial<H3Error>): H3Error {
3029 }
3130
3231 const err = new H3Error ( input . message )
33- Error . captureStackTrace ( err , createError )
3432
3533 if ( input . statusCode ) {
3634 err . statusCode = input . statusCode
3735 }
36+
3837 if ( input . statusMessage ) {
3938 err . statusMessage = input . statusMessage
4039 }
40+
4141 if ( input . data ) {
4242 err . data = input . data
4343 }
44- if ( input . internal ) {
45- err . internal = input . internal
46- }
44+
4745 return err
4846}
4947
@@ -58,11 +56,16 @@ export function createError (input: Partial<H3Error>): H3Error {
5856 * In the debug mode the stack trace of errors will be return in response.
5957 */
6058export function sendError ( res : ServerResponse , error : Error | H3Error , debug ?: boolean ) {
61- const h3Error = createError ( error )
59+ let h3Error : H3Error
60+ if ( error instanceof H3Error ) {
61+ h3Error = error
62+ } else {
63+ console . error ( error ) // eslint-disable-line no-console
64+ h3Error = createError ( error )
65+ }
6266
63- // @ts -ignore
64- if ( h3Error . internal ) {
65- console . error ( h3Error ) // eslint-disable-line no-console
67+ if ( res . writableEnded ) {
68+ return
6669 }
6770
6871 res . statusCode = h3Error . statusCode
0 commit comments