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

18 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Annotated 1abc

2 

3from fastapi import Depends, FastAPI, HTTPException 1abc

4 

5app = FastAPI() 1abc

6 

7 

8class InternalError(Exception): 1abc

9 pass 1abc

10 

11 

12def get_username(): 1abc

13 try: 1mjdenkfgolhi

14 yield "Rick" 1mjdenkfgolhi

15 except InternalError: 1jdekfglhi

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

17 raise 1defghi

18 

19 

20@app.get("/items/{item_id}") 1abc

21def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): 1abc

22 if item_id == "portal-gun": 1mjdenkfgolhi

23 raise InternalError( 1defghi

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

25 ) 

26 if item_id != "plumbus": 1mjnkol

27 raise HTTPException( 1jkl

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

29 ) 

30 return item_id 1mno