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' ;
@@ -49,7 +50,6 @@ describe('Class: Router - Error Handling', () => {
49
50
const app = new Router ( ) ;
50
51
51
52
app . notFound ( async ( error ) => ( {
52
- statusCode : HttpErrorCodes . NOT_FOUND ,
53
53
error : 'Not Found' ,
54
54
message : `Custom: ${ error . message } ` ,
55
55
} ) ) ;
@@ -64,9 +64,9 @@ describe('Class: Router - Error Handling', () => {
64
64
expect ( result ) . toEqual ( {
65
65
statusCode : HttpErrorCodes . NOT_FOUND ,
66
66
body : JSON . stringify ( {
67
- statusCode : HttpErrorCodes . NOT_FOUND ,
68
67
error : 'Not Found' ,
69
68
message : 'Custom: Route /nonexistent for method GET not found' ,
69
+ statusCode : HttpErrorCodes . NOT_FOUND ,
70
70
} ) ,
71
71
headers : { 'content-type' : 'application/json' } ,
72
72
isBase64Encoded : false ,
@@ -78,7 +78,6 @@ describe('Class: Router - Error Handling', () => {
78
78
const app = new Router ( ) ;
79
79
80
80
app . methodNotAllowed ( async ( error ) => ( {
81
- statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
82
81
error : 'Method Not Allowed' ,
83
82
message : `Custom: ${ error . message } ` ,
84
83
} ) ) ;
@@ -94,9 +93,9 @@ describe('Class: Router - Error Handling', () => {
94
93
expect ( result ) . toEqual ( {
95
94
statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
96
95
body : JSON . stringify ( {
97
- statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
98
96
error : 'Method Not Allowed' ,
99
97
message : 'Custom: POST not allowed' ,
98
+ statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
100
99
} ) ,
101
100
headers : { 'content-type' : 'application/json' } ,
102
101
isBase64Encoded : false ,
@@ -393,4 +392,178 @@ describe('Class: Router - Error Handling', () => {
393
392
expect ( body . hasEvent ) . toBe ( true ) ;
394
393
expect ( body . hasContext ) . toBe ( true ) ;
395
394
} ) ;
395
+
396
+ it ( 'handles returning a Response from the error handler' , async ( ) => {
397
+ // Prepare
398
+ const app = new Router ( ) ;
399
+
400
+ app . errorHandler (
401
+ BadRequestError ,
402
+ async ( ) =>
403
+ new Response (
404
+ JSON . stringify ( {
405
+ foo : 'bar' ,
406
+ } ) ,
407
+ {
408
+ status : HttpErrorCodes . BAD_REQUEST ,
409
+ headers : {
410
+ 'content-type' : 'application/json' ,
411
+ } ,
412
+ }
413
+ )
414
+ ) ;
415
+
416
+ app . get ( '/test' , ( ) => {
417
+ throw new BadRequestError ( 'test error' ) ;
418
+ } ) ;
419
+
420
+ // Act
421
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
422
+
423
+ // Assess
424
+ expect ( result ) . toEqual ( {
425
+ statusCode : HttpErrorCodes . BAD_REQUEST ,
426
+ body : JSON . stringify ( {
427
+ foo : 'bar' ,
428
+ } ) ,
429
+ headers : { 'content-type' : 'application/json' } ,
430
+ isBase64Encoded : false ,
431
+ } ) ;
432
+ } ) ;
433
+
434
+ it ( 'handles returning an API Gateway Proxy result from the error handler' , async ( ) => {
435
+ // Prepare
436
+ const app = new Router ( ) ;
437
+
438
+ app . errorHandler ( BadRequestError , async ( ) => ( {
439
+ statusCode : HttpErrorCodes . BAD_REQUEST ,
440
+ body : JSON . stringify ( {
441
+ foo : 'bar' ,
442
+ } ) ,
443
+ } ) ) ;
444
+
445
+ app . get ( '/test' , ( ) => {
446
+ throw new BadRequestError ( 'test error' ) ;
447
+ } ) ;
448
+
449
+ // Act
450
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
451
+
452
+ // Assess
453
+ expect ( result ) . toEqual ( {
454
+ statusCode : HttpErrorCodes . BAD_REQUEST ,
455
+ body : JSON . stringify ( {
456
+ foo : 'bar' ,
457
+ } ) ,
458
+ } ) ;
459
+ } ) ;
460
+
461
+ it ( 'handles returning a JSONObject from the error handler' , async ( ) => {
462
+ // Prepare
463
+ const app = new Router ( ) ;
464
+
465
+ app . errorHandler ( BadRequestError , async ( ) => ( { foo : 'bar' } ) ) ;
466
+
467
+ app . get ( '/test' , ( ) => {
468
+ throw new BadRequestError ( 'test error' ) ;
469
+ } ) ;
470
+
471
+ // Act
472
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
473
+
474
+ // Assess
475
+ expect ( result ) . toEqual ( {
476
+ statusCode : HttpErrorCodes . INTERNAL_SERVER_ERROR ,
477
+ body : JSON . stringify ( {
478
+ foo : 'bar' ,
479
+ } ) ,
480
+ headers : { 'content-type' : 'application/json' } ,
481
+ isBase64Encoded : false ,
482
+ } ) ;
483
+ } ) ;
484
+
485
+ it ( 'handles throwing a built in NotFound error from the error handler' , async ( ) => {
486
+ // Prepare
487
+ const app = new Router ( ) ;
488
+
489
+ app . errorHandler ( BadRequestError , async ( ) => {
490
+ throw new NotFoundError ( 'This error is thrown from the error handler' ) ;
491
+ } ) ;
492
+
493
+ app . get ( '/test' , ( ) => {
494
+ throw new BadRequestError ( 'test error' ) ;
495
+ } ) ;
496
+
497
+ // Act
498
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
499
+
500
+ // Assess
501
+ expect ( result ) . toEqual ( {
502
+ statusCode : HttpErrorCodes . NOT_FOUND ,
503
+ body : JSON . stringify ( {
504
+ statusCode : HttpErrorCodes . NOT_FOUND ,
505
+ error : 'NotFoundError' ,
506
+ message : 'This error is thrown from the error handler' ,
507
+ } ) ,
508
+ headers : { 'content-type' : 'application/json' } ,
509
+ isBase64Encoded : false ,
510
+ } ) ;
511
+ } ) ;
512
+
513
+ it ( 'handles throwing a built in MethodNotAllowedError error from the error handler' , async ( ) => {
514
+ // Prepare
515
+ const app = new Router ( ) ;
516
+
517
+ app . errorHandler ( BadRequestError , async ( ) => {
518
+ throw new MethodNotAllowedError (
519
+ 'This error is thrown from the error handler'
520
+ ) ;
521
+ } ) ;
522
+
523
+ app . get ( '/test' , ( ) => {
524
+ throw new BadRequestError ( 'test error' ) ;
525
+ } ) ;
526
+
527
+ // Act
528
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
529
+
530
+ // Assess
531
+ expect ( result ) . toEqual ( {
532
+ statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
533
+ body : JSON . stringify ( {
534
+ statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
535
+ error : 'MethodNotAllowedError' ,
536
+ message : 'This error is thrown from the error handler' ,
537
+ } ) ,
538
+ headers : { 'content-type' : 'application/json' } ,
539
+ isBase64Encoded : false ,
540
+ } ) ;
541
+ } ) ;
542
+
543
+ it ( 'handles throwing a generic error from the error handler' , async ( ) => {
544
+ // Prepare
545
+ vi . stubEnv ( 'POWERTOOLS_DEV' , 'true' ) ;
546
+ const app = new Router ( ) ;
547
+
548
+ app . errorHandler ( BadRequestError , async ( ) => {
549
+ throw new Error ( 'This error is thrown from the error handler' ) ;
550
+ } ) ;
551
+
552
+ app . get ( '/test' , ( ) => {
553
+ throw new BadRequestError ( 'test error' ) ;
554
+ } ) ;
555
+
556
+ // Act
557
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' ) , context ) ;
558
+
559
+ // Assess
560
+ expect ( result . statusCode ) . toBe ( HttpErrorCodes . INTERNAL_SERVER_ERROR ) ;
561
+ const body = JSON . parse ( result . body ) ;
562
+ expect ( body . error ) . toBe ( 'Internal Server Error' ) ;
563
+ expect ( body . message ) . toBe ( 'This error is thrown from the error handler' ) ;
564
+ expect ( body . stack ) . toBeDefined ( ) ;
565
+ expect ( body . details ) . toEqual ( {
566
+ errorName : 'Error' ,
567
+ } ) ;
568
+ } ) ;
396
569
} ) ;
0 commit comments