Skip to content

Commit 019e099

Browse files
author
Michael Brewer
committed
feat: add example for custom service errors
1 parent f0bae3c commit 019e099

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,8 @@ def include_router(self, router: "Router", prefix: Optional[str] = None) -> None
679679

680680
self.route(*route)(func)
681681

682-
def not_found(self):
683-
return self.exception_handler(404)
682+
def not_found(self, func: Callable):
683+
return self.exception_handler(404)(func)
684684

685685
def exception_handler(self, exc_class_or_status_code: Union[int, Type[Exception]]):
686686
def register_exception_handler(func: Callable):

tests/functional/event_handler/test_api_gateway.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ def foo():
10801080

10811081
def test_exception_handler():
10821082
# GIVEN a resolver with an exception handler defined for ValueError
1083-
app = ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEvent)
1083+
app = ApiGatewayResolver()
10841084

10851085
@app.exception_handler(ValueError)
10861086
def handle_value_error(ex: ValueError):
@@ -1105,17 +1105,44 @@ def get_lambda() -> Response:
11051105
assert result["body"] == "Foo!"
11061106

11071107

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+
11081135
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()
11111138

1112-
@app.not_found()
1113-
def handle_not_found(exc: NotFoundError):
1139+
@app.not_found
1140+
def handle_not_found(exc: NotFoundError) -> Response:
11141141
assert isinstance(exc, NotFoundError)
11151142
return Response(status_code=404, content_type=content_types.TEXT_PLAIN, body="I am a teapot!")
11161143

11171144
# WHEN calling the event handler
1118-
# AND a ValueError is raised
1145+
# AND not route is found
11191146
result = app(LOAD_GW_EVENT, {})
11201147

11211148
# THEN call the exception_handler

0 commit comments

Comments
 (0)