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

Payments Error Contract

easy—No data yet

Problem statement

Build the payment service's refusals and the one envelope every failure is reported in, so a missing payment, an unknown account, a frozen sender, a self payment, a shortfall and a malformed request all answer in the same shape with their own code.

Sample Data

The ledger in app/store.py holds five accounts, ops with 500, payroll with 1200, vendor-pool with 80, legal-hold with 640 and dormant with 15, the last two frozen. It holds three payments under the ids 1_, _2 and 4_, so _3 and 50 belong to no payment, and the next payment created takes the id 5.

API Endpoints to Implement

The refusals live in app/services/payments.py and the envelope in app/core/errors.py, which is handed the application so the conversions are registered on it rather than written into any endpoint.

GET /payments/{payment_id}

  • payment_id comes from the URL as a whole number.
  • Answers 200 with the payment, holding id, sender, recipient and amount.
  • A payment the ledger does not hold is refused with 404 and the message Payment 3 was not found.

POST /payments

Takes the body as a PaymentCreate, holding sender, recipient and amount, all required, and answers 201 with the stored payment. The amount leaves the sender and reaches the recipient, so each of their balances moves by it.

The refusals are checked in this order, and the first one decides the answer:

  • An account neither name matches is refused with 404 and the message Account ghost was not found, the sender resolved before the recipient.
  • A payment from an account to itself is refused with 400 and the message Account ops cannot pay itself.
  • A frozen sender is refused with 403, the message Account legal-hold is frozen, and a header X-Frozen-Account naming the account. Only this refusal carries that header. A frozen account may still receive.
  • An amount the sender's balance does not cover is refused with 409 and the message Account ops holds 500, a payment of 501 was refused. A payment of the whole balance is allowed.

A refused payment moves no money.

Rejected requests

Every failure is answered in one envelope: a single key error, holding exactly code, message and details.

  • code is not_found for a 404, forbidden for a 403, self_payment for a 400, insufficient_funds for a 409 and validation_error for a 422.
  • message is the refusal's own message, and for a 422 it is always the text Request validation failed.
  • details is an empty list for every refusal above. For a 422 it holds one entry per broken rule, each holding exactly field, the path to the offending value with its parts joined by dots such as body.amount or path.payment_id, message, the reason the framework gave, and code, the name the framework gave the rule.

The envelope does not depend on how deep the refusal was raised: a refusal from the endpoint's own lookup and one from further inside the service answer identically.

Sample Requests & Responses

POST /payments

Request:

{
  "sender": "ops",
  "recipient": "payroll",
  "amount": 200
}

Response (201 Created):

{
  "id": 5,
  "sender": "ops",
  "recipient": "payroll",
  "amount": 200
}

GET /payments/3

Response (404 Not Found):

{
  "error": {
    "code": "not_found",
    "message": "Payment 3 was not found",
    "details": []
  }
}

POST /payments, sending a sender of legal-hold, answered with an X-Frozen-Account of legal-hold

Response (403 Forbidden):

{
  "error": {
    "code": "forbidden",
    "message": "Account legal-hold is frozen",
    "details": []
  }
}

POST /payments, sending an amount of 501 from ops

Response (409 Conflict):

{
  "error": {
    "code": "insufficient_funds",
    "message": "Account ops holds 500, a payment of 501 was refused",
    "details": []
  }
}

POST /payments

Request:

{}

Response (422 Unprocessable Entity):

{
  "error": {
    "code": "validation_error",
    "message": "Request validation failed",
    "details": [
      {"field": "body.sender", "message": "Field required", "code": "missing"},
      {"field": "body.recipient", "message": "Field required", "code": "missing"},
      {"field": "body.amount", "message": "Field required", "code": "missing"}
    ]
  }
}

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 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 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.