@@ -1007,7 +1007,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
1007
1007
// Prepare
1008
1008
const app = new AppSyncGraphQLResolver ( ) ;
1009
1009
1010
- class TestService {
1010
+ class Lambda {
1011
1011
@app . exceptionHandler ( ValidationError )
1012
1012
async handleValidationError ( error : ValidationError ) {
1013
1013
return {
@@ -1017,9 +1017,24 @@ describe('Class: AppSyncGraphQLResolver', () => {
1017
1017
} ;
1018
1018
}
1019
1019
1020
+ @app . exceptionHandler ( NotFoundError )
1021
+ handleNotFoundError ( error : NotFoundError ) {
1022
+ return {
1023
+ message : 'Decorator user not found' ,
1024
+ details : error . message ,
1025
+ type : 'decorator_user_not_found' ,
1026
+ } ;
1027
+ }
1028
+
1020
1029
@app . onQuery ( 'getUser' )
1021
- async getUser ( ) {
1022
- throw new ValidationError ( 'Decorator error test' ) ;
1030
+ async getUser ( { id, name } : { id : string ; name : string } ) {
1031
+ if ( ! id ) {
1032
+ throw new ValidationError ( 'Decorator error test' ) ;
1033
+ }
1034
+ if ( id === '0' ) {
1035
+ throw new NotFoundError ( `User with ID ${ id } not found` ) ;
1036
+ }
1037
+ return { id, name } ;
1023
1038
}
1024
1039
1025
1040
async handler ( event : unknown , context : Context ) {
@@ -1029,20 +1044,30 @@ describe('Class: AppSyncGraphQLResolver', () => {
1029
1044
}
1030
1045
}
1031
1046
1032
- const service = new TestService ( ) ;
1047
+ const lambda = new Lambda ( ) ;
1048
+ const handler = lambda . handler . bind ( lambda ) ;
1033
1049
1034
1050
// Act
1035
- const result = await service . handler (
1051
+ const validationError = await handler (
1036
1052
onGraphqlEventFactory ( 'getUser' , 'Query' , { } ) ,
1037
1053
context
1038
1054
) ;
1055
+ const notFoundError = await handler (
1056
+ onGraphqlEventFactory ( 'getUser' , 'Query' , { id : '0' , name : 'John Doe' } ) ,
1057
+ context
1058
+ ) ;
1039
1059
1040
1060
// Assess
1041
- expect ( result ) . toEqual ( {
1061
+ expect ( validationError ) . toEqual ( {
1042
1062
message : 'Decorator validation failed' ,
1043
1063
details : 'Decorator error test' ,
1044
1064
type : 'decorator_validation_error' ,
1045
1065
} ) ;
1066
+ expect ( notFoundError ) . toEqual ( {
1067
+ message : 'Decorator user not found' ,
1068
+ details : 'User with ID 0 not found' ,
1069
+ type : 'decorator_user_not_found' ,
1070
+ } ) ;
1046
1071
} ) ;
1047
1072
1048
1073
// #endregion Exception handling
0 commit comments