StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 6 · Error Handling

Domain HTTP Exceptions

easy—No data yet

Problem statement

Build a case reader that hands an open case to anyone, keeps a sealed case to its lead alone, and stops the request with the right status, message and header when the case is missing or off limits.

Sample Data

The archive in app/store.py holds five cases under the ids 1_, _2, 4_, _6 and 9_, so _3 and 50 belong to no case. Three of them are sealed, and two leads hold more than one case. Here is one case record:

{
  "id": 2,
  "title": "Missing Inventory",
  "lead": "kai",
  "sealed": true
}

API Endpoints to Implement

fetch_case in app/services/cases.py locates the case and then decides whether the requester may read it. The route hands it a case_id from the URL and a requester from the query, text, optional, default anonymous.

  • A case the archive does not hold stops the read with 404 and the body {"detail": "Case 3 was not found"}, naming the id that was asked for.
  • A sealed case read by anyone but its lead stops the read with 403, the body {"detail": "Case 2 is sealed"} naming the case, and a header X-Sealed-By holding the lead's name.
  • The requester must equal the lead exactly, letter case included, so Kai is not kai.
  • An open case is read by anyone, named or not, and a sealed case is read by its lead.
  • Response code: 200. The body is the case, holding id, title, lead and sealed.
  • Neither a successful read nor the 404 carries X-Sealed-By.
  • Both rejections stop the service itself rather than being answered by the route, so calling fetch_case directly raises them, carrying the same status, message and header. A rejected read leaves the archive readable.
Sample Requests & Responses

GET /cases/1, naming no requester

Response (200 OK):

{
  "id": 1,
  "title": "Warehouse Audit",
  "lead": "rowan",
  "sealed": false
}

GET /cases/2?requester=kai, the lead reading their own sealed case

Response (200 OK):

{
  "id": 2,
  "title": "Missing Inventory",
  "lead": "kai",
  "sealed": true
}

GET /cases/2?requester=rowan, a sealed case read by someone else, answered with an X-Sealed-By of kai

Response (403 Forbidden):

{
  "detail": "Case 2 is sealed"
}

GET /cases/3

Response (404 Not Found):

{
  "detail": "Case 3 was not found"
}

More in Error Handling

See the module
Custom Exception HandlerThis problem covers giving one kind of business failure its own answer in a single place, so every endpoint that can hit it reports the same thing and no other failure is disturbed.0%0 of 1 solvedeasyReshaped Validation ErrorsThis problem covers replacing the framework's account of what a caller got wrong with the service's own, naming each bad value by where it sits and keeping the reason it was refused.—No data yeteasyPayments Error ContractThis problem covers giving a payments service one way of reporting every refusal, so whatever went wrong and however deep it happened, a caller reads the same shape and can tell the causes apart.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.