Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log handled errors to warning #1926

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion connexion/middleware/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions connexion/validators/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to configure this, as I'd quite like it as a warning; but not an error, and it's clear to me from this that many people may have different use-cases. I Know I can redirect a log-stream.

f"Validation error: {exception.message}{error_path_msg}",
extra={"validator": "body"},
)
Expand All @@ -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."""
Expand Down Expand Up @@ -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"},
)
Expand Down
Loading