]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛Fix `RuntimeError` raised when `HTTPException` has a status code with no content...
authorIrfanuddin Shafi Ahmed <irfanudeen08@gmail.com>
Sun, 11 Sep 2022 16:13:36 +0000 (21:43 +0530)
committerGitHub <noreply@github.com>
Sun, 11 Sep 2022 16:13:36 +0000 (16:13 +0000)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
fastapi/exception_handlers.py
tests/test_starlette_exception.py

index 2b286d71c7610ca87f6e37c9bc3fa99cca2b0416..4d7ea5ec2e44bab45b444881175403685a5876fc 100644 (file)
@@ -1,19 +1,19 @@
 from fastapi.encoders import jsonable_encoder
 from fastapi.exceptions import RequestValidationError
+from fastapi.utils import is_body_allowed_for_status_code
 from starlette.exceptions import HTTPException
 from starlette.requests import Request
-from starlette.responses import JSONResponse
+from starlette.responses import JSONResponse, Response
 from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
 
 
-async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
+async def http_exception_handler(request: Request, exc: HTTPException) -> Response:
     headers = getattr(exc, "headers", None)
-    if headers:
-        return JSONResponse(
-            {"detail": exc.detail}, status_code=exc.status_code, headers=headers
-        )
-    else:
-        return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)
+    if not is_body_allowed_for_status_code(exc.status_code):
+        return Response(status_code=exc.status_code, headers=headers)
+    return JSONResponse(
+        {"detail": exc.detail}, status_code=exc.status_code, headers=headers
+    )
 
 
 async def request_validation_exception_handler(
index 859169d3cdad8d7ebe6275976793d1665f1ca95d..2b6712f7b6c5875ff853cabd7ad489084b407007 100644 (file)
@@ -18,6 +18,16 @@ async def read_item(item_id: str):
     return {"item": items[item_id]}
 
 
+@app.get("/http-no-body-statuscode-exception")
+async def no_body_status_code_exception():
+    raise HTTPException(status_code=204)
+
+
+@app.get("/http-no-body-statuscode-with-detail-exception")
+async def no_body_status_code_with_detail_exception():
+    raise HTTPException(status_code=204, detail="I should just disappear!")
+
+
 @app.get("/starlette-items/{item_id}")
 async def read_starlette_item(item_id: str):
     if item_id not in items:
@@ -31,6 +41,30 @@ openapi_schema = {
     "openapi": "3.0.2",
     "info": {"title": "FastAPI", "version": "0.1.0"},
     "paths": {
+        "/http-no-body-statuscode-exception": {
+            "get": {
+                "operationId": "no_body_status_code_exception_http_no_body_statuscode_exception_get",
+                "responses": {
+                    "200": {
+                        "content": {"application/json": {"schema": {}}},
+                        "description": "Successful " "Response",
+                    }
+                },
+                "summary": "No Body " "Status " "Code " "Exception",
+            }
+        },
+        "/http-no-body-statuscode-with-detail-exception": {
+            "get": {
+                "operationId": "no_body_status_code_with_detail_exception_http_no_body_statuscode_with_detail_exception_get",
+                "responses": {
+                    "200": {
+                        "content": {"application/json": {"schema": {}}},
+                        "description": "Successful " "Response",
+                    }
+                },
+                "summary": "No Body Status Code With Detail Exception",
+            }
+        },
         "/items/{item_id}": {
             "get": {
                 "responses": {
@@ -154,3 +188,15 @@ def test_get_starlette_item_not_found():
     assert response.status_code == 404, response.text
     assert response.headers.get("x-error") is None
     assert response.json() == {"detail": "Item not found"}
+
+
+def test_no_body_status_code_exception_handlers():
+    response = client.get("/http-no-body-statuscode-exception")
+    assert response.status_code == 204
+    assert not response.content
+
+
+def test_no_body_status_code_with_detail_exception_handlers():
+    response = client.get("/http-no-body-statuscode-with-detail-exception")
+    assert response.status_code == 204
+    assert not response.content