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(
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:
"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": {
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