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

Reshaped Validation Errors

easy—No data yet

Problem statement

Build the three registration endpoints and replace the framework's validation report with one of the service's own, naming each offending value by its path and keeping the reason and the rule it broke.

Sample Data

The register in app/store.py starts with four registrations under the ids 1_ to _4, three of them for workshop 101 and one for 102. The next registration created takes the id 5.

API Endpoints to Implement

The three handlers live in app/api/routes/registrations.py, and their helpers, list_registrations, registrations_for and add_registration, in app/services/registrations.py.

GET /registrations

Answers 200 with every registration in the order the register holds them, each carrying id, attendee, workshop_id and seats.

GET /workshops/{workshop_id}/registrations

  • workshop_id comes from the URL as a whole number.
  • Answers 200 with only that workshop's registrations, and with an empty list for a workshop nobody registered for.

POST /registrations

Takes the body as a RegistrationCreate, holding attendee, workshop_id and seats, all required, and answers 201 with the stored registration: the three drafted values plus the id it was given.

Rejected requests

reshape_validation_errors in app/core/errors.py answers every validation failure, on the body and on the URL alike, with 422 and a body holding exactly two keys: error, always the text validation_error, and errors, one entry per broken rule.

Each entry holds exactly three keys:

  • field, the path to the offending value with its parts joined by dots, so a body field is body.attendee and the number in the URL is path.workshop_id.
  • message, the reason the framework gave.
  • code, the name the framework gave the rule.

A draft breaking three rules reports three entries, and nothing is dropped: the original report can be rebuilt from the reshaped one.

The framework must be left to find the failures rather than each handler checking its own, since the service publishes the contract at GET /openapi.json, where the registration requires a JSON body, both validated operations declare 422, and workshop_id is a required whole number in the path.

Sample Requests & Responses

POST /registrations

Request:

{
  "attendee": "Noa Lindt",
  "workshop_id": 102,
  "seats": 2
}

Response (201 Created):

{
  "id": 5,
  "attendee": "Noa Lindt",
  "workshop_id": 102,
  "seats": 2
}

GET /workshops/101/registrations

Response (200 OK):

[
  {"id": 1, "attendee": "Mira Chen", "workshop_id": 101, "seats": 2},
  {"id": 3, "attendee": "Sam Ortiz", "workshop_id": 101, "seats": 3},
  {"id": 4, "attendee": "Rae Patel", "workshop_id": 101, "seats": 1}
]

POST /registrations

Request:

{
  "attendee": 12,
  "seats": "many"
}

Response (422 Unprocessable Entity):

{
  "error": "validation_error",
  "errors": [
    {"field": "body.attendee", "message": "Input should be a valid string", "code": "string_type"},
    {"field": "body.workshop_id", "message": "Field required", "code": "missing"},
    {"field": "body.seats", "message": "Input should be a valid integer, unable to parse string as an integer", "code": "int_parsing"}
  ]
}

GET /workshops/abc/registrations

Response (422 Unprocessable Entity):

{
  "error": "validation_error",
  "errors": [
    {"field": "path.workshop_id", "message": "Input should be a valid integer, unable to parse string as an integer", "code": "int_parsing"}
  ]
}

More in Error Handling

See the module
Domain HTTP ExceptionsThis problem covers stopping a request from deep inside the service when the thing asked for is missing or off limits, so the caller gets the right status, a clear reason and a hint of who to ask.—No data yeteasyCustom 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 solvedeasyPayments 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.