From e9fd5387d1d8e614f3b3849647a530903edf98f3 Mon Sep 17 00:00:00 2001 From: Evgeniy Lupashin <34810566+PipeKnight@users.noreply.github.com> Date: Tue, 30 Aug 2022 16:47:24 +0300 Subject: [PATCH] Simplify code of http_exception_handler Looking to `JSONResponse.__init__()` function, you can see that `headers` parameter is `None` by default, so it doesn't matter do we pass it here as `None` or do not pass at all. That's why we can safely remove this if-else expression and slightly simplify the code --- fastapi/exception_handlers.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/fastapi/exception_handlers.py b/fastapi/exception_handlers.py index 2b286d71c7610..12ffd7d59ad05 100644 --- a/fastapi/exception_handlers.py +++ b/fastapi/exception_handlers.py @@ -7,13 +7,11 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse: - 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) + return JSONResponse( + {"detail": exc.detail}, + status_code=exc.status_code, + headers=getattr(exc, "headers", None), + ) async def request_validation_exception_handler(