@@ -59,7 +59,7 @@ export class App<
59
59
attach : ( req : Req , res : Res , next : NextFunction ) => void
60
60
61
61
// this symbol tells if a custom error handler has been set
62
- private readonly [ hasSetCustomErrorHandler ] : boolean
62
+ [ hasSetCustomErrorHandler ] : boolean
63
63
64
64
constructor ( options : AppConstructor < Req , Res > = { } ) {
65
65
super ( )
@@ -261,11 +261,9 @@ export class App<
261
261
await applyHandler < Req , Res > ( handler ) ( req , res , next )
262
262
}
263
263
264
- let idx = 0 , err : unknown | undefined = null
264
+ let idx = 0 , err : unknown | undefined
265
265
const next : NextFunction = async ( error ) => {
266
- if ( error ) {
267
- err = error
268
- }
266
+ if ( error ) err = error
269
267
return await loop ( )
270
268
}
271
269
const loop = async ( ) =>
@@ -275,12 +273,12 @@ export class App<
275
273
276
274
await loop ( )
277
275
278
- if ( err instanceof Response ) return err
276
+ if ( err instanceof Response ) throw err
279
277
else if ( err ) {
280
- if ( this [ hasSetCustomErrorHandler ] ) return await this . onError ( err , req )
281
- else return err
278
+ if ( this [ hasSetCustomErrorHandler ] ) throw await this . onError ( err , req )
279
+ else throw err
282
280
}
283
- return new Response ( res . _body , res . _init )
281
+ throw new Response ( res . _body , res . _init )
284
282
}
285
283
286
284
handler = async ( _req : Request , connInfo ?: ConnInfo ) => {
@@ -302,12 +300,14 @@ export class App<
302
300
locals : { } ,
303
301
}
304
302
305
-
306
- const k = await this . #prepare( req , res )
307
-
308
-
309
- if ( k instanceof Response ) return k
310
- return await this . onError ( k , req )
303
+ let err
304
+ try {
305
+ await this . #prepare( req , res )
306
+ } catch ( error ) {
307
+ err = error
308
+ }
309
+ if ( err instanceof Response ) return err
310
+ return await this . onError ( err , req )
311
311
}
312
312
/**
313
313
* Creates HTTP server and dispatches middleware
0 commit comments