From 810a980fe99423013130ff1df4ca9d727e1e9109 Mon Sep 17 00:00:00 2001 From: alfechner Date: Mon, 27 May 2024 11:21:04 +0200 Subject: [PATCH] Log handled errors to warning (#1926) Fixes #1925. Changes proposed in this pull request: - Log handled errors to `warning` instead of `error`. - Log validation errors to `info` because the intent of the log lines in informational. The error is handled by raising a new error. --------- Co-authored-by: Alex Fechner --- connexion/middleware/exceptions.py | 13 ++++++++++++- connexion/validators/json.py | 7 ++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/connexion/middleware/exceptions.py b/connexion/middleware/exceptions.py index 33688b840..25dfd17da 100644 --- a/connexion/middleware/exceptions.py +++ b/connexion/middleware/exceptions.py @@ -72,7 +72,12 @@ def add_exception_handler( @staticmethod def problem_handler(_request: ConnexionRequest, exc: ProblemException): """Default handler for Connexion ProblemExceptions""" - logger.error("%r", exc) + + if 400 <= exc.status <= 499: + logger.warning("%r", exc) + else: + logger.error("%r", exc) + return exc.to_problem() @staticmethod @@ -81,6 +86,12 @@ def http_exception( _request: StarletteRequest, exc: HTTPException, **kwargs ) -> StarletteResponse: """Default handler for Starlette HTTPException""" + + if 400 <= exc.status_code <= 499: + logger.warning("%r", exc) + else: + logger.error("%r", exc) + logger.error("%r", exc) return problem( title=http_facts.HTTP_STATUS_CODES.get(exc.status_code), diff --git a/connexion/validators/json.py b/connexion/validators/json.py index 5babce5ff..acfd96525 100644 --- a/connexion/validators/json.py +++ b/connexion/validators/json.py @@ -68,7 +68,7 @@ def _validate(self, body: t.Any) -> t.Optional[dict]: return self._validator.validate(body) except ValidationError as exception: error_path_msg = format_error_with_path(exception=exception) - logger.error( + logger.info( f"Validation error: {exception.message}{error_path_msg}", extra={"validator": "body"}, ) @@ -77,7 +77,8 @@ def _validate(self, body: t.Any) -> t.Optional[dict]: class DefaultsJSONRequestBodyValidator(JSONRequestBodyValidator): """Request body validator for json content types which fills in default values. This Validator - intercepts the body, makes changes to it, and replays it for the next ASGI application.""" + intercepts the body, makes changes to it, and replays it for the next ASGI application. + """ MUTABLE_VALIDATION = True """This validator might mutate to the body.""" @@ -129,7 +130,7 @@ def _validate(self, body: dict): self.validator.validate(body) except ValidationError as exception: error_path_msg = format_error_with_path(exception=exception) - logger.error( + logger.warning( f"Validation error: {exception.message}{error_path_msg}", extra={"validator": "body"}, )