Skip to content

Commit c169812

Browse files
committed
chore(event_handler): enable exception handler when using validation
1 parent 129f93e commit c169812

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from aws_lambda_powertools.event_handler import content_types
3333
from aws_lambda_powertools.event_handler.exceptions import NotFoundError, ServiceError
3434
from aws_lambda_powertools.event_handler.openapi.constants import DEFAULT_API_VERSION, DEFAULT_OPENAPI_VERSION
35+
from aws_lambda_powertools.event_handler.openapi.exceptions import RequestValidationError
3536
from aws_lambda_powertools.event_handler.openapi.swagger_ui.html import generate_swagger_html
3637
from aws_lambda_powertools.event_handler.openapi.types import (
3738
COMPONENT_REF_PREFIX,
@@ -1972,6 +1973,17 @@ def _call_exception_handler(self, exp: Exception, route: Route) -> Optional[Resp
19721973
except ServiceError as service_error:
19731974
exp = service_error
19741975

1976+
if isinstance(exp, RequestValidationError):
1977+
return self._response_builder_class(
1978+
response=Response(
1979+
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
1980+
content_type=content_types.APPLICATION_JSON,
1981+
body={"statusCode": HTTPStatus.UNPROCESSABLE_ENTITY, "message": exp.errors()},
1982+
),
1983+
serializer=self._serializer,
1984+
route=route,
1985+
)
1986+
19751987
if isinstance(exp, ServiceError):
19761988
return self._response_builder_class(
19771989
response=Response(

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

+33-40
Original file line numberDiff line numberDiff line change
@@ -62,50 +62,43 @@ def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) ->
6262
values: Dict[str, Any] = {}
6363
errors: List[Any] = []
6464

65-
try:
66-
# Process path values, which can be found on the route_args
67-
path_values, path_errors = _request_params_to_args(
68-
route.dependant.path_params,
69-
app.context["_route_args"],
65+
# Process path values, which can be found on the route_args
66+
path_values, path_errors = _request_params_to_args(
67+
route.dependant.path_params,
68+
app.context["_route_args"],
69+
)
70+
71+
# Process query values
72+
query_values, query_errors = _request_params_to_args(
73+
route.dependant.query_params,
74+
app.current_event.query_string_parameters or {},
75+
)
76+
77+
values.update(path_values)
78+
values.update(query_values)
79+
errors += path_errors + query_errors
80+
81+
# Process the request body, if it exists
82+
if route.dependant.body_params:
83+
(body_values, body_errors) = _request_body_to_args(
84+
required_params=route.dependant.body_params,
85+
received_body=self._get_body(app),
7086
)
87+
values.update(body_values)
88+
errors.extend(body_errors)
7189

72-
# Process query values
73-
query_values, query_errors = _request_params_to_args(
74-
route.dependant.query_params,
75-
app.current_event.query_string_parameters or {},
76-
)
77-
78-
values.update(path_values)
79-
values.update(query_values)
80-
errors += path_errors + query_errors
90+
if errors:
91+
# Raise the validation errors
92+
raise RequestValidationError(_normalize_errors(errors))
93+
else:
94+
# Re-write the route_args with the validated values, and call the next middleware
95+
app.context["_route_args"] = values
8196

82-
# Process the request body, if it exists
83-
if route.dependant.body_params:
84-
(body_values, body_errors) = _request_body_to_args(
85-
required_params=route.dependant.body_params,
86-
received_body=self._get_body(app),
87-
)
88-
values.update(body_values)
89-
errors.extend(body_errors)
97+
# Call the handler by calling the next middleware
98+
response = next_middleware(app)
9099

91-
if errors:
92-
# Raise the validation errors
93-
raise RequestValidationError(_normalize_errors(errors))
94-
else:
95-
# Re-write the route_args with the validated values, and call the next middleware
96-
app.context["_route_args"] = values
97-
98-
# Call the handler by calling the next middleware
99-
response = next_middleware(app)
100-
101-
# Process the response
102-
return self._handle_response(route=route, response=response)
103-
except RequestValidationError as e:
104-
return Response(
105-
status_code=422,
106-
content_type="application/json",
107-
body=json.dumps({"detail": e.errors()}),
108-
)
100+
# Process the response
101+
return self._handle_response(route=route, response=response)
109102

110103
def _handle_response(self, *, route: Route, response: Response):
111104
# Process the response body if it exists

0 commit comments

Comments
 (0)