File tree Expand file tree Collapse file tree 3 files changed +46
-5
lines changed Expand file tree Collapse file tree 3 files changed +46
-5
lines changed Original file line number Diff line number Diff line change @@ -331,6 +331,12 @@ class Router {
331
331
headers : { 'Content-Type' : 'application/json' } ,
332
332
} ) ;
333
333
} catch ( handlerError ) {
334
+ if ( handlerError instanceof NotFoundError ) {
335
+ return await this . handleError ( handlerError , options ) ;
336
+ }
337
+ if ( handlerError instanceof MethodNotAllowedError ) {
338
+ return await this . handleError ( handlerError , options ) ;
339
+ }
334
340
return this . #defaultErrorHandler( handlerError as Error ) ;
335
341
}
336
342
}
Original file line number Diff line number Diff line change @@ -12,11 +12,17 @@ import type { Route } from '../rest/Route.js';
12
12
import type { Router } from '../rest/Router.js' ;
13
13
import type { ResolveOptions } from './common.js' ;
14
14
15
- type ErrorResponse = {
16
- statusCode : HttpStatusCode ;
17
- error : string ;
18
- message : string ;
19
- } ;
15
+ type ErrorResponse =
16
+ | {
17
+ statusCode : HttpStatusCode ;
18
+ error : string ;
19
+ message ?: string ;
20
+ }
21
+ | {
22
+ statusCode : HttpStatusCode ;
23
+ error ?: string ;
24
+ message : string ;
25
+ } ;
20
26
21
27
type RequestContext = {
22
28
req : Request ;
Original file line number Diff line number Diff line change 5
5
HttpErrorCodes ,
6
6
InternalServerError ,
7
7
MethodNotAllowedError ,
8
+ NotFoundError ,
8
9
Router ,
9
10
} from '../../../../src/rest/index.js' ;
10
11
import { createTestEvent } from '../helpers.js' ;
@@ -429,4 +430,32 @@ describe('Class: Router - Error Handling', () => {
429
430
isBase64Encoded : false ,
430
431
} ) ;
431
432
} ) ;
433
+
434
+ it ( 'handles throwing a built in error from the error handler' , async ( ) => {
435
+ // Prepare
436
+ const app = new Router ( ) ;
437
+
438
+ app . errorHandler ( BadRequestError , async ( ) => {
439
+ throw new NotFoundError ( 'This error is thrown from the error handler' ) ;
440
+ } ) ;
441
+
442
+ app . get ( '/test' , ( ) => {
443
+ throw new BadRequestError ( 'test error' ) ;
444
+ } ) ;
445
+
446
+ // Act
447
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
448
+
449
+ // Assess
450
+ expect ( result ) . toEqual ( {
451
+ statusCode : HttpErrorCodes . NOT_FOUND ,
452
+ body : JSON . stringify ( {
453
+ statusCode : HttpErrorCodes . NOT_FOUND ,
454
+ error : 'NotFoundError' ,
455
+ message : 'This error is thrown from the error handler' ,
456
+ } ) ,
457
+ headers : { 'content-type' : 'application/json' } ,
458
+ isBase64Encoded : false ,
459
+ } ) ;
460
+ } ) ;
432
461
} ) ;
You can’t perform that action at this time.
0 commit comments