Skip to content

Commit 69ed880

Browse files
committed
feat: enhance error handling in AppSyncGraphQLResolver with NotFoundError decorator
1 parent 3a7d8dc commit 69ed880

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

packages/event-handler/tests/unit/appsync-graphql/AppSyncGraphQLResolver.test.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
10071007
// Prepare
10081008
const app = new AppSyncGraphQLResolver();
10091009

1010-
class TestService {
1010+
class Lambda {
10111011
@app.exceptionHandler(ValidationError)
10121012
async handleValidationError(error: ValidationError) {
10131013
return {
@@ -1017,9 +1017,24 @@ describe('Class: AppSyncGraphQLResolver', () => {
10171017
};
10181018
}
10191019

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+
10201029
@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 };
10231038
}
10241039

10251040
async handler(event: unknown, context: Context) {
@@ -1029,20 +1044,30 @@ describe('Class: AppSyncGraphQLResolver', () => {
10291044
}
10301045
}
10311046

1032-
const service = new TestService();
1047+
const lambda = new Lambda();
1048+
const handler = lambda.handler.bind(lambda);
10331049

10341050
// Act
1035-
const result = await service.handler(
1051+
const validationError = await handler(
10361052
onGraphqlEventFactory('getUser', 'Query', {}),
10371053
context
10381054
);
1055+
const notFoundError = await handler(
1056+
onGraphqlEventFactory('getUser', 'Query', { id: '0', name: 'John Doe' }),
1057+
context
1058+
);
10391059

10401060
// Assess
1041-
expect(result).toEqual({
1061+
expect(validationError).toEqual({
10421062
message: 'Decorator validation failed',
10431063
details: 'Decorator error test',
10441064
type: 'decorator_validation_error',
10451065
});
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+
});
10461071
});
10471072

10481073
// #endregion Exception handling

0 commit comments

Comments
 (0)