Skip to content

Commit

Permalink
Merge branch 'main' into msgspec-signature-model
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut authored Jun 21, 2023
2 parents 10d88e8 + a8498fd commit 0188b6c
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 22 deletions.
10 changes: 8 additions & 2 deletions litestar/middleware/exceptions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ def create_exception_response(exc: Exception) -> Response:
Returns:
Response: HTTP response constructed from exception details.
"""
status_code = getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR)
if status_code == HTTP_500_INTERNAL_SERVER_ERROR:
detail = "Internal Server Error"
else:
detail = getattr(exc, "detail", repr(exc))

content = ExceptionResponseContent(
status_code=getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR),
detail=getattr(exc, "detail", repr(exc)),
status_code=status_code,
detail=detail,
headers=getattr(exc, "headers", None),
extra=getattr(exc, "extra", None),
)
Expand Down
1 change: 0 additions & 1 deletion tests/examples/test_dto/test_example_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def test_dto_data_problem_statement_app() -> None:
with TestClient(app) as client:
response = client.post("/person", json={"name": "John", "age": 30})
assert response.status_code == 500
assert "missing 1 required positional argument: 'id'" in response.json()["detail"]


def test_dto_data_usage_app() -> None:
Expand Down
1 change: 0 additions & 1 deletion tests/examples/test_dto/test_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def test_read_only_fields():
response = client.post("/person", json={"name": "peter", "age": 40, "email": "email_of_peter@example.com"})

assert response.status_code == 500
assert "__init__() missing 1 required positional argument: 'id'" in response.json()["detail"]


def test_dto_data():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

pytestmark = [
pytest.mark.skipif(platform.uname()[4] != "x86_64", reason="oracle not available on this platform"),
pytest.mark.sqlalchemy_integration,
]


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_create_exception_response_utility_non_http_exception() -> None:
response = create_exception_response(exc)
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.media_type == MediaType.JSON
assert response.content == {"status_code": 500, "detail": "RuntimeError('yikes')"}
assert response.content == {"status_code": 500, "detail": "Internal Server Error"}


def test_missing_dependency_exception() -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_kwargs/test_generator_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def handler(dep: str) -> Dict[str, str]:
with create_test_client(route_handlers=[handler]) as client:
res = client.get("/")
assert res.status_code == 500
assert res.json() == {"detail": "ValueError('foo')", "status_code": 500}
assert res.json() == {"detail": "Internal Server Error", "status_code": 500}
cleanup_mock.assert_not_called()
exception_mock.assert_called_once()
finally_mock.assert_called_once()
Expand All @@ -144,7 +144,7 @@ def handler(dep: str) -> Dict[str, str]:
with create_test_client(route_handlers=[handler]) as client:
res = client.get("/")
assert res.status_code == 500
assert res.json() == {"status_code": 500, "detail": "Exception('foo')"}
assert res.json() == {"status_code": 500, "detail": "Internal Server Error"}
cleanup_mock.assert_called_once()
finally_mock.assert_called_once()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def http_route_handler_user_scope(request: Request[User, None, Any]) -> None:
client = create_test_client(route_handlers=[http_route_handler_user_scope])
error_response = client.get("/", headers={"Authorization": "nope"})
assert error_response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert error_response.json()["detail"] == "'user' is not defined in scope, install an AuthMiddleware to set it"


def test_authentication_middleware_not_installed_raises_for_auth_scope_http() -> None:
Expand All @@ -83,7 +82,6 @@ def http_route_handler_auth_scope(request: Request[None, Auth, Any]) -> None:
client = create_test_client(route_handlers=[http_route_handler_auth_scope])
error_response = client.get("/", headers={"Authorization": "nope"})
assert error_response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert error_response.json()["detail"] == "'auth' is not defined in scope, install an AuthMiddleware to set it"


@websocket(path="/")
Expand Down
24 changes: 15 additions & 9 deletions tests/unit/test_middleware/test_exception_handler_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_default_handle_http_exception_handling_extra_object() -> None:
)
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.content == {
"detail": "litestar_exception",
"detail": "Internal Server Error",
"extra": {"key": "value"},
"status_code": 500,
}
Expand All @@ -52,7 +52,7 @@ def test_default_handle_http_exception_handling_extra_none() -> None:
HTTPException(detail="litestar_exception"),
)
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.content == {"detail": "litestar_exception", "status_code": 500}
assert response.content == {"detail": "Internal Server Error", "status_code": 500}


def test_default_handle_litestar_http_exception_handling() -> None:
Expand All @@ -61,7 +61,7 @@ def test_default_handle_litestar_http_exception_handling() -> None:
HTTPException(detail="litestar_exception"),
)
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.content == {"detail": "litestar_exception", "status_code": 500}
assert response.content == {"detail": "Internal Server Error", "status_code": 500}


def test_default_handle_litestar_http_exception_extra_list() -> None:
Expand All @@ -71,7 +71,7 @@ def test_default_handle_litestar_http_exception_extra_list() -> None:
)
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.content == {
"detail": "litestar_exception",
"detail": "Internal Server Error",
"extra": ["extra-1", "extra-2"],
"status_code": 500,
}
Expand All @@ -84,7 +84,7 @@ def test_default_handle_starlette_http_exception_handling() -> None:
)
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.content == {
"detail": "litestar_exception",
"detail": "Internal Server Error",
"status_code": 500,
}

Expand All @@ -95,7 +95,7 @@ def test_default_handle_python_http_exception_handling() -> None:
)
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.content == {
"detail": repr(AttributeError("oops")),
"detail": "Internal Server Error",
"status_code": HTTP_500_INTERNAL_SERVER_ERROR,
}

Expand Down Expand Up @@ -164,7 +164,10 @@ def handler() -> None:
client.app.logger = get_logger("litestar")
response = client.get("/test")
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert "Test debug exception" in response.text
if is_debug:
assert "Test debug exception" in response.text
else:
assert "Internal Server Error" in response.text

if should_log:
assert len(caplog.records) == 1
Expand Down Expand Up @@ -206,7 +209,10 @@ def handler() -> None:
with TestClient(app=app) as client, capture_logs() as cap_logs:
response = client.get("/test")
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert "Test debug exception" in response.text
if is_debug:
assert "Test debug exception" in response.text
else:
assert "Internal Server Error" in response.text

if should_log:
assert len(cap_logs) == 1
Expand All @@ -233,7 +239,7 @@ def handler() -> None:
client.app.logger = get_logger("litestar")
response = client.get("/test")
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert "Test debug exception" in response.text
assert "Internal Server Error" in response.text

assert len(caplog.records) == 1
assert caplog.records[0].levelname == "ERROR"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_middleware/test_session/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def handler(request: Request) -> None:
with create_test_client(handler) as client:
response = client.get("/test")
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert response.json()["detail"] == "'session' is not defined in scope, install a SessionMiddleware to set it"
assert response.json()["detail"] == "Internal Server Error"


def test_integration(session_backend_config: "BaseBackendConfig") -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_signature/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test(dep: int, param: int, optional_dep: Optional[int] = Dependency()) -> No
response = client.get("/?param=13")

assert response.json() == {
"detail": "A dependency failed validation for GET http://testserver.local/?param=13",
"detail": "Internal Server Error",
"extra": error_extra,
"status_code": HTTP_500_INTERNAL_SERVER_ERROR,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_template/test_built_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def invalid_template_name_handler() -> Template:
with create_test_client(route_handlers=[invalid_template_name_handler], template_config=template_config) as client:
response = client.request("GET", "/")
assert response.status_code == 500
assert response.json() == {"detail": "Template invalid.html not found.", "status_code": 500}
assert response.json() == {"detail": "Internal Server Error", "status_code": 500}


def test_no_context(tmp_path: Path, template_config: TemplateConfig) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_template/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def invalid_path() -> Template:
with create_test_client(route_handlers=[invalid_path]) as client:
response = client.request("GET", "/")
assert response.status_code == 500
assert response.json() == {"detail": "Template engine is not configured", "status_code": 500}
assert response.json() == {"detail": "Internal Server Error", "status_code": 500}


def test_engine_passed_to_callback(tmp_path: "Path") -> None:
Expand Down

0 comments on commit 0188b6c

Please sign in to comment.