Skip to content

Commit

Permalink
fix: invalid use of isinstance for HTTP exceptions checking (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorasful committed Apr 1, 2024
1 parent e473460 commit b0f22e9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/app/lib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ def exception_to_http_response(
http_exc = PermissionDeniedException
else:
http_exc = InternalServerException
if request.app.debug and not isinstance(http_exc, PermissionDeniedException | NotFoundError | AuthorizationError):
if request.app.debug and http_exc not in (PermissionDeniedException, NotFoundError, AuthorizationError):
return create_debug_response(request, exc)
return create_exception_response(request, http_exc(detail=str(exc.__cause__)))
11 changes: 6 additions & 5 deletions tests/unit/lib/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ def test_repository_exception_to_http_response(exc: type[ApplicationError], stat


@pytest.mark.parametrize(
("exc", "status"),
("exc", "status", "debug"),
[
(exceptions.AuthorizationError, HTTP_403_FORBIDDEN),
(exceptions.ApplicationError, HTTP_500_INTERNAL_SERVER_ERROR),
(exceptions.AuthorizationError, HTTP_403_FORBIDDEN, True),
(exceptions.AuthorizationError, HTTP_403_FORBIDDEN, False),
(exceptions.ApplicationError, HTTP_500_INTERNAL_SERVER_ERROR, False),
],
)
def test_exception_to_http_response(exc: type[exceptions.ApplicationError], status: int) -> None:
app = Litestar(route_handlers=[])
def test_exception_to_http_response(exc: type[exceptions.ApplicationError], status: int, debug: bool) -> None:
app = Litestar(route_handlers=[], debug=debug)
request = RequestFactory(app=app, server="testserver").get("/wherever")
response = exceptions.exception_to_http_response(request, exc())
assert response.status_code == status
Expand Down

0 comments on commit b0f22e9

Please sign in to comment.