Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
fix(logging): don't log exceptions that are http and < 500 status
Browse files Browse the repository at this point in the history
  • Loading branch information
peterschutt committed Nov 17, 2022
1 parent bce531d commit 6b42e48
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import pytest
from starlite import Starlite, get
from starlite.exceptions import ValidationException
from starlite.status_codes import (
HTTP_400_BAD_REQUEST,
HTTP_403_FORBIDDEN,
HTTP_404_NOT_FOUND,
HTTP_409_CONFLICT,
Expand Down Expand Up @@ -43,6 +45,25 @@ def raises() -> None:
logger_mock.assert_called_once_with(exc_info=(RuntimeError, exc, ANY))


def test_after_exception_hook_handler_doesnt_log_400(monkeypatch: pytest.MonkeyPatch) -> None:
"""Tests that the handler doesn't call the logger if 400 exception."""
logger_mock = MagicMock()
monkeypatch.setattr(exceptions, "bind_contextvars", logger_mock)
exc = ValidationException()

@get("/error")
def raises() -> None:
raise exc

with create_test_client(
route_handlers=[raises], after_exception=exceptions.after_exception_hook_handler
) as client:
resp = client.get("/error")
assert resp.status_code == HTTP_400_BAD_REQUEST

logger_mock.assert_not_called()


@pytest.mark.parametrize(
("exc", "status"),
[
Expand Down

0 comments on commit 6b42e48

Please sign in to comment.