@@ -1080,7 +1080,7 @@ def foo():
1080
1080
1081
1081
def test_exception_handler ():
1082
1082
# GIVEN a resolver with an exception handler defined for ValueError
1083
- app = ApiGatewayResolver (proxy_type = ProxyEventType . APIGatewayProxyEvent )
1083
+ app = ApiGatewayResolver ()
1084
1084
1085
1085
@app .exception_handler (ValueError )
1086
1086
def handle_value_error (ex : ValueError ):
@@ -1105,17 +1105,44 @@ def get_lambda() -> Response:
1105
1105
assert result ["body" ] == "Foo!"
1106
1106
1107
1107
1108
+ def test_exception_handler_service_error ():
1109
+ # GIVEN
1110
+ app = ApiGatewayResolver ()
1111
+
1112
+ @app .exception_handler (ServiceError )
1113
+ def service_error (ex : ServiceError ):
1114
+ print (ex .msg )
1115
+ return Response (
1116
+ status_code = ex .status_code ,
1117
+ content_type = content_types .APPLICATION_JSON ,
1118
+ body = "CUSTOM ERROR FORMAT" ,
1119
+ )
1120
+
1121
+ @app .get ("/my/path" )
1122
+ def get_lambda () -> Response :
1123
+ raise InternalServerError ("Something sensitive" )
1124
+
1125
+ # WHEN calling the event handler
1126
+ # AND a ServiceError is raised
1127
+ result = app (LOAD_GW_EVENT , {})
1128
+
1129
+ # THEN call the exception_handler
1130
+ assert result ["statusCode" ] == 500
1131
+ assert result ["headers" ]["Content-Type" ] == content_types .APPLICATION_JSON
1132
+ assert result ["body" ] == "CUSTOM ERROR FORMAT"
1133
+
1134
+
1108
1135
def test_exception_handler_not_found ():
1109
- # GIVEN a resolver with an exception handler defined for ValueError
1110
- app = ApiGatewayResolver (proxy_type = ProxyEventType . APIGatewayProxyEvent )
1136
+ # GIVEN a resolver with an exception handler defined for a 404 not found
1137
+ app = ApiGatewayResolver ()
1111
1138
1112
- @app .not_found ()
1113
- def handle_not_found (exc : NotFoundError ):
1139
+ @app .not_found
1140
+ def handle_not_found (exc : NotFoundError ) -> Response :
1114
1141
assert isinstance (exc , NotFoundError )
1115
1142
return Response (status_code = 404 , content_type = content_types .TEXT_PLAIN , body = "I am a teapot!" )
1116
1143
1117
1144
# WHEN calling the event handler
1118
- # AND a ValueError is raised
1145
+ # AND not route is found
1119
1146
result = app (LOAD_GW_EVENT , {})
1120
1147
1121
1148
# THEN call the exception_handler
0 commit comments