
Module 7 Exercises: Communicating with Confidence
Practical exercises to master HTTP exceptions, custom handlers, and standardized response envelopes.
Module 7 Exercises: Communicating with Confidence
A great API doesn't just work; it communicates. These exercises will help you build an API that provides clarity in both success and failure.
Exercise 1: The Precision Exception
Write a FastAPI endpoint @app.get("/items/{item_id}") that:
- Takes a string
item_id. - If the id starts with
"legacy-", raise a410 Goneexception with the message"This resource has been retired." - Otherwise, return the ID.
Exercise 2: Mapping Logic to Errors
You have a Python function in your "Services" layer that raises a standard DatabaseConnectionError.
class DatabaseConnectionError(Exception):
pass
Write a global Exception Handler for your FastAPI app that catches this error and returns a 503 Service Unavailable status code with a JSON body saying: "The database is currently undergoing maintenance. Please try again in 5 minutes."
Exercise 3: Designing the Paginated Contract
Design a Pydantic model for a Paginated List of Products.
- The "Envelope" should contain the list of products.
- The "Metadata" should contain
total,current_page, and a booleanhas_more.
Self-Correction / Discussion
Exercise 1 Answer:
@app.get("/items/{item_id}")
async def get_item(item_id: str):
if item_id.startswith("legacy-"):
raise HTTPException(status_code=410, detail="This resource has been retired.")
return {"id": item_id}
Exercise 2 Answer:
@app.exception_handler(DatabaseConnectionError)
async def db_error_handler(request: Request, exc: DatabaseConnectionError):
return JSONResponse(
status_code=503,
content={"detail": "The database is currently undergoing maintenance. Please try again in 5 minutes."}
)
Exercise 3 Answer:
class Product(BaseModel):
name: str
price: float
class PaginatedProducts(BaseModel):
items: list[Product]
total: int
current_page: int
has_more: bool
Summary of Module 7
You have mastered the "Voice" of your API.
- Exceptions: You can signal errors instantly.
- Custom Handlers: You can map logic to HTTP standards globally.
- Consistent Formats: You ensure the frontend always knows what to expect.
In Module 8: Middleware and Request Lifecycle, we will look at the "Interceptors"—the code that runs before and after every single request.