55 HttpErrorCodes ,
66 InternalServerError ,
77 MethodNotAllowedError ,
8+ NotFoundError ,
89 Router ,
910} from '../../../../src/rest/index.js' ;
1011import { createTestEvent } from '../helpers.js' ;
@@ -49,7 +50,6 @@ describe('Class: Router - Error Handling', () => {
4950 const app = new Router ( ) ;
5051
5152 app . notFound ( async ( error ) => ( {
52- statusCode : HttpErrorCodes . NOT_FOUND ,
5353 error : 'Not Found' ,
5454 message : `Custom: ${ error . message } ` ,
5555 } ) ) ;
@@ -64,9 +64,9 @@ describe('Class: Router - Error Handling', () => {
6464 expect ( result ) . toEqual ( {
6565 statusCode : HttpErrorCodes . NOT_FOUND ,
6666 body : JSON . stringify ( {
67- statusCode : HttpErrorCodes . NOT_FOUND ,
6867 error : 'Not Found' ,
6968 message : 'Custom: Route /nonexistent for method GET not found' ,
69+ statusCode : HttpErrorCodes . NOT_FOUND ,
7070 } ) ,
7171 headers : { 'content-type' : 'application/json' } ,
7272 isBase64Encoded : false ,
@@ -78,7 +78,6 @@ describe('Class: Router - Error Handling', () => {
7878 const app = new Router ( ) ;
7979
8080 app . methodNotAllowed ( async ( error ) => ( {
81- statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
8281 error : 'Method Not Allowed' ,
8382 message : `Custom: ${ error . message } ` ,
8483 } ) ) ;
@@ -94,9 +93,9 @@ describe('Class: Router - Error Handling', () => {
9493 expect ( result ) . toEqual ( {
9594 statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
9695 body : JSON . stringify ( {
97- statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
9896 error : 'Method Not Allowed' ,
9997 message : 'Custom: POST not allowed' ,
98+ statusCode : HttpErrorCodes . METHOD_NOT_ALLOWED ,
10099 } ) ,
101100 headers : { 'content-type' : 'application/json' } ,
102101 isBase64Encoded : false ,
@@ -393,4 +392,178 @@ describe('Class: Router - Error Handling', () => {
393392 expect ( body . hasEvent ) . toBe ( true ) ;
394393 expect ( body . hasContext ) . toBe ( true ) ;
395394 } ) ;
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+ } ) ;
396569} ) ;
0 commit comments