Skip to content

Commit

Permalink
fix: handle no body status codes in HTTPExceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
iudeen committed Sep 8, 2022
1 parent 6620273 commit afecf9b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions fastapi/exception_handlers.py
@@ -1,13 +1,16 @@
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 not is_body_allowed_for_status_code(exc.status_code):
return Response(status_code=exc.status_code, headers=exc.headers)
if headers:
return JSONResponse(
{"detail": exc.detail}, status_code=exc.status_code, headers=headers
Expand Down

0 comments on commit afecf9b

Please sign in to comment.