Coverage for docs_src/dependencies/tutorial008d.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from fastapi import Depends, FastAPI, HTTPException 1abcdefg

2 

3app = FastAPI() 1abcdefg

4 

5 

6class InternalError(Exception): 1abcdefg

7 pass 1abcdefg

8 

9 

10def get_username(): 1abcdefg

11 try: 1CvhiDwjkExlmFynoGzpqHArsIBtu

12 yield "Rick" 1CvhiDwjkExlmFynoGzpqHArsIBtu

13 except InternalError: 1vhiwjkxlmynozpqArsBtu

14 print("We don't swallow the internal error here, we raise again 😎") 1hijklmnopqrstu

15 raise 1hijklmnopqrstu

16 

17 

18@app.get("/items/{item_id}") 1abcdefg

19def get_item(item_id: str, username: str = Depends(get_username)): 1abcdefg

20 if item_id == "portal-gun": 1CvhiDwjkExlmFynoGzpqHArsIBtu

21 raise InternalError( 1hijklmnopqrstu

22 f"The portal gun is too dangerous to be owned by {username}" 

23 ) 

24 if item_id != "plumbus": 1CvDwExFyGzHAIB

25 raise HTTPException( 1vwxyzAB

26 status_code=404, detail="Item not found, there's only a plumbus here" 

27 ) 

28 return item_id 1CDEFGHI