@@ -981,3 +981,125 @@ async def get_user(event: List) -> List:
981
981
# THEN the resolver must be able to return a field in the batch_current_event
982
982
assert app .context == {}
983
983
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 ]
0 commit comments