link
fastapi/raise-404

Raise HTTPException

Return error responses

error
handling

Command

Examples

Raise 404 error

from fastapi import HTTPException
  
  @app.get("/items/{item_id}")
  def read_item(item_id: str):
      item = db.get(item_id)
      if not item:
          raise HTTPException(status_code=404, detail="Item not found")
      return item