Skip to content

Commit 03f12b7

Browse files
authored
Allow HTTPException to use non-standard status codes (#3545)
1 parent 76fd00f commit 03f12b7

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎docs/exceptions.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ The `ExceptionMiddleware` implementation defaults to returning plain-text HTTP r
120120

121121
* `HTTPException(status_code, detail=None, headers=None)`
122122

123+
When `detail` is not provided, it defaults to the standard phrase for the status code. If no standard phrase exists,
124+
it defaults to an empty string.
125+
123126
You should only raise `HTTPException` inside routing or endpoints.
124127
Middleware classes should instead just return appropriate responses directly.
125128

‎starlette/exceptions.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

3-
import http
3+
import http.client
44
from collections.abc import Mapping
55

66

77
class HTTPException(Exception):
88
def __init__(self, status_code: int, detail: str | None = None, headers: Mapping[str, str] | None = None) -> None:
99
if detail is None:
10-
detail = http.HTTPStatus(status_code).phrase
10+
detail = http.client.responses.get(status_code, "")
1111
self.status_code = status_code
1212
self.detail = detail
1313
self.headers = headers

‎tests/test_exceptions.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def with_headers(request: Request) -> None:
3434
raise HTTPException(status_code=200, headers={"x-potato": "always"})
3535

3636

37+
def non_standard_status_code(request: Request) -> None:
38+
raise HTTPException(status_code=499)
39+
40+
3741
class BadBodyException(HTTPException):
3842
pass
3943

@@ -62,6 +66,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
6266
Route("/no_content", endpoint=no_content),
6367
Route("/not_modified", endpoint=not_modified),
6468
Route("/with_headers", endpoint=with_headers),
69+
Route("/non_standard_status_code", endpoint=non_standard_status_code),
6570
Route("/handled_exc_after_response", endpoint=HandledExcAfterResponse()),
6671
WebSocketRoute("/runtime_error", endpoint=raise_runtime_error),
6772
Route("/consume_body_in_endpoint_and_handler", endpoint=read_body_and_raise_exc, methods=["POST"]),
@@ -105,6 +110,12 @@ def test_with_headers(client: TestClient) -> None:
105110
assert response.headers["x-potato"] == "always"
106111

107112

113+
def test_non_standard_status_code(client: TestClient) -> None:
114+
response = client.get("/non_standard_status_code")
115+
assert response.status_code == 499
116+
assert response.text == ""
117+
118+
108119
def test_websockets_should_raise(client: TestClient) -> None:
109120
with pytest.raises(RuntimeError):
110121
with client.websocket_connect("/runtime_error"):

0 commit comments

Comments
 (0)