Skip to content

Lesson 4 of 9 · 24m

Error Design: RFC 9457 and Actionable Failure

Problem Details, machine-readable error codes, validation detail, and error responses clients can actually recover from.

Most API errors are written for the server engineer who already knows what happened. Production clients need the opposite: a stable, machine-readable explanation of what they can do next. "Invalid request" is not an error contract; it is a shrug with a status code.

RFC 9457 gives you the envelope

Use application/problem+json and the standard Problem Details fields:

HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json

{
  "type": "https://api.sudosecurity.example/problems/invoice-not-payable",
  "title": "Invoice cannot be paid",
  "status": 422,
  "detail": "Invoice INV-2026-0417 is already void.",
  "instance": "/invoices/inv_9Zk2/payments",
  "code": "invoice_not_payable",
  "invoice_status": "void"
}

type is a stable identifier, ideally documentation. title is generic for the class; detail can include instance-specific context. Never localize machine fields. Put human locale-sensitive rendering in the client.

Distinguish transport, domain, and validation failures

A missing authentication token is 401; a valid token without permission is 403; a conflict with current state is 409; a syntactically valid request violating business rules is often 422. Clients branch on these categories. If you collapse them all into 400, they cannot implement correct recovery.

{
  "type": "https://api.example.test/problems/validation-failed",
  "title": "Validation failed",
  "status": 422,
  "errors": [
    {"field": "lines[0].quantity", "code": "too_small", "message": "Quantity must be at least 1."},
    {"field": "customer", "code": "not_found", "message": "Customer does not exist."}
  ]
}

Validation should be atomic in reporting: return every independent problem you can find, not just the first. Developers fix more per round trip, and generated forms can mark all invalid fields.

Errors need operational discipline

Do not leak stack traces, SQL, hostnames, internal IDs beyond public identifiers, or whether a hidden resource exists unless your threat model permits enumeration. A 404 can deliberately double as "exists but not for you"; if so, apply it consistently rather than accidentally.

At the same time, include enough correlation to debug. A trace_id or request_id copied from your observability pipeline saves hours:

{"title": "Internal server error", "status": 500, "request_id": "req_01JZQ8..."}

Log full details server-side. Return the minimum useful detail client-side.

Retriability is part of the contract

Clients need to know whether retrying is safe and useful. 429 should include Retry-After when possible. 503 is retryable; 422 is not; 409 may be retryable after re-reading state. If your API uses idempotency keys, say which failures consume the key and which do not.

Field notes

  • Keep error code strings stable forever, even when human text changes.
  • Do not expose validation internals like regex patterns or schema class names unless they are part of the public contract.
  • A problem type URL does not have to resolve today, but it should be stable enough that it could.
  • Test clients against realistic failure responses, not just happy-path fixtures.

Was this lesson helpful?

Sign in to rate