Thanks to visit codestin.com
Credit goes to github.com

Skip to content

✨ Preserve json.JSONDecodeError information when handling invalid JSON in request body, to support custom exception handlers that use its information#4057

Merged
tiangolo merged 3 commits into
fastapi:masterfrom
UKnowWhoIm:master
Aug 25, 2022

Conversation

@UKnowWhoIm

@UKnowWhoIm UKnowWhoIm commented Oct 16, 2021

Copy link
Copy Markdown
Contributor

Save JSONDecodeError to context by adding a from clause. The feature was requested because the user wants to use status code 400 for invalid response body.

Usage

from fastapi.exception_handlers import request_validation_exception_handler
from fastapi.exceptions import RequestValidationError

app = FastAPI()

@app.exception_handler(RequestValidationError)
async def handle(request: Request, exc: Exception):
    if isinstance(exc.__context__, JSONDecodeError):
        return Response(str(exc),status_code=400)
    return await request_validation_exception_handler(request, exc)

Enhancement requested #3993

@tiangolo tiangolo changed the title Preserve json.JSONDecodeError when handling invalid JSON in request body ✨ Preserve json.JSONDecodeError information when handling invalid JSON in request body, to support custom exception handlers that use its information Aug 25, 2022
@codecov

codecov Bot commented Aug 25, 2022

Copy link
Copy Markdown

Codecov Report

Merging #4057 (9809b51) into master (afaa039) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #4057   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          533       533           
  Lines        13755     13755           
=========================================
  Hits         13755     13755           
Impacted Files Coverage Δ
fastapi/routing.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@github-actions

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit 9809b51 at: https://630746e846ee066d37855789--fastapi.netlify.app

@tiangolo

Copy link
Copy Markdown
Member

Awesome, thank you @UKnowWhoIm! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants