Errors Everyone Can Read

Errors Everyone Can Read: Raising Your Own Exceptions in an API, One Error Shape for the Whole API.

Updated

  1. Raising Your Own Exceptions in an API (Journeyman): An API's own rules raise exception classes named for each problem, such as TicketNotFound and StatusChangeNotAllowed, from plain functions that know nothing about HTTP, and @app.exception_handler turns each one into a status code and a JSON body, such as 404 or 409.
  2. One Error Shape for the Whole API (Journeyman): Every error an API sends can have one JSON shape, modeled on RFC 9457's problem details (a status, a title, a detail sentence and, on a 422, a list of field problems), built by one problem() function and sent by an exception handler for each kind of error, FastAPI's own 404 and 422 included, with a catch-all that answers a crash with a calm 500 and never a traceback.