Coverage for docs_src/handling_errors/tutorial005.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import FastAPI, Request, status 1abcde

2from fastapi.encoders import jsonable_encoder 1abcde

3from fastapi.exceptions import RequestValidationError 1abcde

4from fastapi.responses import JSONResponse 1abcde

5from pydantic import BaseModel 1abcde

6 

7app = FastAPI() 1abcde

8 

9 

10@app.exception_handler(RequestValidationError) 1abcde

11async def validation_exception_handler(request: Request, exc: RequestValidationError): 1abcde

12 return JSONResponse( 1abcde

13 status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, 

14 content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}), 

15 ) 

16 

17 

18class Item(BaseModel): 1abcde

19 title: str 1abcde

20 size: int 1abcde

21 

22 

23@app.post("/items/") 1abcde

24async def create_item(item: Item): 1abcde

25 return item 1abcde