Coverage for docs_src/dependencies/tutorial008d.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1from fastapi import Depends, FastAPI, HTTPException 1abcdef
3app = FastAPI() 1abcdef
6class InternalError(Exception): 1abcdef
7 pass 1abcdef
10def get_username(): 1abcdef
11 try: 1ysghztijAuklBvmnCwopDxqr
12 yield "Rick" 1ysghztijAuklBvmnCwopDxqr
13 except InternalError: 1sghtijuklvmnwopxqr
14 print("We don't swallow the internal error here, we raise again 😎") 1ghijklmnopqr
15 raise 1ghijklmnopqr
18@app.get("/items/{item_id}") 1abcdef
19def get_item(item_id: str, username: str = Depends(get_username)): 1abcdef
20 if item_id == "portal-gun": 1ysghztijAuklBvmnCwopDxqr
21 raise InternalError( 1ghijklmnopqr
22 f"The portal gun is too dangerous to be owned by {username}"
23 )
24 if item_id != "plumbus": 1ysztAuBvCwDx
25 raise HTTPException( 1stuvwx
26 status_code=404, detail="Item not found, there's only a plumbus here"
27 )
28 return item_id 1yzABCD