HTTPException

noun

say it: “AYCH-TEE-TEE-PEE-ik-SEP-shun” /ˌeɪtʃˌtiˌtiˌpiɪkˈsɛpʃən/

A FastAPI error you raise to stop a route and send back an error response with a status code and a message, such as 404 and "Recipe not found".

What is HTTPException?

Raise HTTPException to stop and send an error response with a status code and message.

from fastapi import HTTPException

recipe = find_recipe(recipe_id)
if recipe is None:
    raise HTTPException(status_code=404, detail='Recipe not found')

In a sentence

  • “The HTTPException's detail text, "Recipe not found", is what the caller sees in the JSON.”
  • “If no recipe has that id, the route raises an HTTPException with status 404.”

Level: Apprentice · More FastAPI terms at this level

Updated