HTTPException - standard
Corefrom fastapi import HTTPException, status
@app.get("/items/{id}")
async def get_item(id: int):
item = db.get(id)
if not item:
raise HTTPException(status_code=404, detail=f"Item {id} not found")
return item
# With extra headers
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"}
)
# Rich detail (dict)
raise HTTPException(
status_code=400,
detail={"code": "DUPLICATE_EMAIL", "field": "email"}
)