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

14 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from fastapi import FastAPI, Request 1abcd

2from fastapi.responses import JSONResponse 1abcd

3 

4 

5class UnicornException(Exception): 1abcd

6 def __init__(self, name: str): 1abcd

7 self.name = name 1efgh

8 

9 

10app = FastAPI() 1abcd

11 

12 

13@app.exception_handler(UnicornException) 1abcd

14async def unicorn_exception_handler(request: Request, exc: UnicornException): 1abcd

15 return JSONResponse( 1efgh

16 status_code=418, 

17 content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."}, 

18 ) 

19 

20 

21@app.get("/unicorns/{name}") 1abcd

22async def read_unicorn(name: str): 1abcd

23 if name == "yolo": 1eifjgkhl

24 raise UnicornException(name=name) 1efgh

25 return {"unicorn_name": name} 1ijkl