Skip to content

Commit 11bd6f9

Browse files
Adding exception handler support - fix tests
1 parent fadeef9 commit 11bd6f9

File tree

3 files changed

+144
-149
lines changed

3 files changed

+144
-149
lines changed

tests/functional/event_handler/required_dependencies/appsync/test_appsync.py

Lines changed: 0 additions & 149 deletions
This file was deleted.

tests/functional/event_handler/required_dependencies/appsync/test_appsync_batch_resolvers.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,3 +981,125 @@ async def get_user(event: List) -> List:
981981
# THEN the resolver must be able to return a field in the batch_current_event
982982
assert app.context == {}
983983
assert ret[0] == "powertools"
984+
985+
986+
def test_exception_handler_with_batch_resolver_and_raise_exception():
987+
988+
# GIVEN a AppSyncResolver instance
989+
app = AppSyncResolver()
990+
991+
event = [
992+
{
993+
"typeName": "Query",
994+
"info": {
995+
"fieldName": "listLocations",
996+
"parentTypeName": "Post",
997+
},
998+
"fieldName": "listLocations",
999+
"arguments": {},
1000+
"source": {
1001+
"id": "1",
1002+
},
1003+
},
1004+
{
1005+
"typeName": "Query",
1006+
"info": {
1007+
"fieldName": "listLocations",
1008+
"parentTypeName": "Post",
1009+
},
1010+
"fieldName": "listLocations",
1011+
"arguments": {},
1012+
"source": {
1013+
"id": "2",
1014+
},
1015+
},
1016+
{
1017+
"typeName": "Query",
1018+
"info": {
1019+
"fieldName": "listLocations",
1020+
"parentTypeName": "Post",
1021+
},
1022+
"fieldName": "listLocations",
1023+
"arguments": {},
1024+
"source": {
1025+
"id": [3, 4],
1026+
},
1027+
},
1028+
]
1029+
1030+
# WHEN we configure exception handler for ValueError
1031+
@app.exception_handler(ValueError)
1032+
def handle_value_error(ex: ValueError):
1033+
return {"message": "error"}
1034+
1035+
# WHEN the sync batch resolver for the 'listLocations' field is defined with raise_on_error=True
1036+
@app.batch_resolver(field_name="listLocations", raise_on_error=True, aggregate=False)
1037+
def create_something(event: AppSyncResolverEvent) -> Optional[list]: # noqa AA03 VNE003
1038+
raise ValueError
1039+
1040+
# Call the implicit handler
1041+
result = app(event, {})
1042+
1043+
# THEN the return must be the Exception Handler error message
1044+
assert result["message"] == "error"
1045+
1046+
1047+
def test_exception_handler_with_batch_resolver_and_no_raise_exception():
1048+
1049+
# GIVEN a AppSyncResolver instance
1050+
app = AppSyncResolver()
1051+
1052+
event = [
1053+
{
1054+
"typeName": "Query",
1055+
"info": {
1056+
"fieldName": "listLocations",
1057+
"parentTypeName": "Post",
1058+
},
1059+
"fieldName": "listLocations",
1060+
"arguments": {},
1061+
"source": {
1062+
"id": "1",
1063+
},
1064+
},
1065+
{
1066+
"typeName": "Query",
1067+
"info": {
1068+
"fieldName": "listLocations",
1069+
"parentTypeName": "Post",
1070+
},
1071+
"fieldName": "listLocations",
1072+
"arguments": {},
1073+
"source": {
1074+
"id": "2",
1075+
},
1076+
},
1077+
{
1078+
"typeName": "Query",
1079+
"info": {
1080+
"fieldName": "listLocations",
1081+
"parentTypeName": "Post",
1082+
},
1083+
"fieldName": "listLocations",
1084+
"arguments": {},
1085+
"source": {
1086+
"id": [3, 4],
1087+
},
1088+
},
1089+
]
1090+
1091+
# WHEN we configure exception handler for ValueError
1092+
@app.exception_handler(ValueError)
1093+
def handle_value_error(ex: ValueError):
1094+
return {"message": "error"}
1095+
1096+
# WHEN the sync batch resolver for the 'listLocations' field is defined with raise_on_error=False
1097+
@app.batch_resolver(field_name="listLocations", raise_on_error=False, aggregate=False)
1098+
def create_something(event: AppSyncResolverEvent) -> Optional[list]: # noqa AA03 VNE003
1099+
raise ValueError
1100+
1101+
# Call the implicit handler
1102+
result = app(event, {})
1103+
1104+
# THEN the return must not trigger the Exception Handler, but instead return from the resolver
1105+
assert result == [None, None, None]

tests/functional/event_handler/required_dependencies/appsync/test_appsync_single_resolvers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,25 @@ async def get_async():
329329
# THEN
330330
assert asyncio.run(result) == "value"
331331
assert app.context == {}
332+
333+
334+
def test_exception_handler_with_single_resolver():
335+
# GIVEN a AppSyncResolver instance
336+
mock_event = load_event("appSyncDirectResolver.json")
337+
338+
app = AppSyncResolver()
339+
340+
# WHEN we configure exception handler for ValueError
341+
@app.exception_handler(ValueError)
342+
def handle_value_error(ex: ValueError):
343+
return {"message": "error"}
344+
345+
@app.resolver(field_name="createSomething")
346+
def create_something(id: str): # noqa AA03 VNE003
347+
raise ValueError("Error")
348+
349+
# Call the implicit handler
350+
result = app(mock_event, {})
351+
352+
# THEN the return must be the Exception Handler error message
353+
assert result["message"] == "error"

0 commit comments

Comments
 (0)