Skip to content

Commit 810a980

Browse files
alfechnerAlex Fechner
andauthored
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 <[email protected]>
1 parent 160a1c0 commit 810a980

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

connexion/middleware/exceptions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ def add_exception_handler(
7272
@staticmethod
7373
def problem_handler(_request: ConnexionRequest, exc: ProblemException):
7474
"""Default handler for Connexion ProblemExceptions"""
75-
logger.error("%r", exc)
75+
76+
if 400 <= exc.status <= 499:
77+
logger.warning("%r", exc)
78+
else:
79+
logger.error("%r", exc)
80+
7681
return exc.to_problem()
7782

7883
@staticmethod
@@ -81,6 +86,12 @@ def http_exception(
8186
_request: StarletteRequest, exc: HTTPException, **kwargs
8287
) -> StarletteResponse:
8388
"""Default handler for Starlette HTTPException"""
89+
90+
if 400 <= exc.status_code <= 499:
91+
logger.warning("%r", exc)
92+
else:
93+
logger.error("%r", exc)
94+
8495
logger.error("%r", exc)
8596
return problem(
8697
title=http_facts.HTTP_STATUS_CODES.get(exc.status_code),

connexion/validators/json.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _validate(self, body: t.Any) -> t.Optional[dict]:
6868
return self._validator.validate(body)
6969
except ValidationError as exception:
7070
error_path_msg = format_error_with_path(exception=exception)
71-
logger.error(
71+
logger.info(
7272
f"Validation error: {exception.message}{error_path_msg}",
7373
extra={"validator": "body"},
7474
)
@@ -77,7 +77,8 @@ def _validate(self, body: t.Any) -> t.Optional[dict]:
7777

7878
class DefaultsJSONRequestBodyValidator(JSONRequestBodyValidator):
7979
"""Request body validator for json content types which fills in default values. This Validator
80-
intercepts the body, makes changes to it, and replays it for the next ASGI application."""
80+
intercepts the body, makes changes to it, and replays it for the next ASGI application.
81+
"""
8182

8283
MUTABLE_VALIDATION = True
8384
"""This validator might mutate to the body."""
@@ -129,7 +130,7 @@ def _validate(self, body: dict):
129130
self.validator.validate(body)
130131
except ValidationError as exception:
131132
error_path_msg = format_error_with_path(exception=exception)
132-
logger.error(
133+
logger.warning(
133134
f"Validation error: {exception.message}{error_path_msg}",
134135
extra={"validator": "body"},
135136
)

0 commit comments

Comments
 (0)