0005. RFC 7807 Problem Details for all error responses
Status
Accepted. Delivered in the REST hardening pass (Task 11, commit "Task 11:
REST API hardening, RFC 7807 errors, OpenAPI 3.0.3 docs, Spectral CI gate").
The audit that motivated it is recorded in REST_AUDIT.md.
Context
The service originally returned a bespoke error body:
{ "error": "requested quantity exceeds usable inventory" }
That shape has three concrete problems once the API has real consumers:
- Nothing is machine-readable. The only signal is an English sentence.
A consumer that wants to distinguish "bin is full" from "reservation exceeds
usable" — both
409 Conflict— has to match on prose, which breaks the first time someone improves the wording. - It is one more dialect to learn. Every service inventing its own error envelope means every client writing per-service error handling.
- The status code alone is not enough. This API returns
409for at least six distinct conditions and422for three, each needing a different reaction from the caller.
At the same time, the REST audit found status-code inaccuracies worth fixing in
the same pass: semantically-invalid values were returning 400 where 422 was
correct, ReceiveStock returned 201 for something with no addressable
resource, and resource creation was missing Location headers.
Constraints:
- The domain must not learn about HTTP. Aggregates return typed errors; only the inbound adapter may know about status codes.
- Whatever is chosen has to be describable in OpenAPI and pass Spectral.
Decision
We will use RFC 7807 Problem Details for HTTP APIs for every error
response, with Content-Type: application/problem+json.
{
"type": "https://errors.inventory-storage.warehouse-systems.dev/insufficient-usable",
"title": "Requested quantity exceeds usable inventory",
"status": 409,
"detail": "requested quantity exceeds usable inventory",
"instance": "/reservations"
}
typeis the machine-readable key — a stable URI, unique per error category. It is an identifier and deliberately need not resolve to a page; the base URIhttps://errors.inventory-storage.warehouse-systems.dev/namespaces it.titleis a fixed human string per category;detailcarries the dynamic message from the underlying typed error;instanceis the request path.- Mapping happens exactly once, in the adapter.
statusFor(err)chooses the status code andproblemFor(err)chooses the(type, title)pair, both ininternal/adapters/inbound/http/errors.go, both switching onerrors.Isagainst typed domain and application errors.statusForis aswitch;problemForis a lookup table (problemCatalog(), an ordered slice of(error, problemInfo)pairs thatproblemForwalks, firsterrors.Ismatch wins — order matters, not a map). The two mirror each other's groupings one-for-one. - Status codes were corrected in the same pass:
422 Unprocessable Entityfor well-formed but semantically invalid values (quantity ≤ 0, negative quantity, invalid bin capacity), leaving400 Bad Requestfor genuinely malformed or missing input;202 AcceptedforReceiveStock— a staged receipt has no id and noGETroute, so201 Createdwould be a lie;201 Createdwith aLocationheader forStowStockandReserveStock;409 Conflictfor state conflicts (bin full, exceeds usable, already resolved, expired).
- The catalog is documented and enforced. Every problem type and every
status code appears in
apis/openapi.yamlas a response example on the operation(s) that can return it — including the slugs written outside the catalog (idempotency-key-required/idempotency-key-reused,counted-quantity-required,invalid-report-query,report-store-error) — and theapi-lintCI job Spectral-lints the spec on every push and pull request.
Consequences
Easier
- Clients can branch on
type. Six different409s become six distinguishable conditions without parsing English. - One dialect across the platform. RFC 7807 is a standard with existing client-side support; nobody has to learn a bespoke envelope.
- The mapping is auditable in one place. A function and a table, side by
side, one case/row per typed error. Adding a domain error without mapping it is visible in
review — and falls back to
internal-error/500rather than leaking. - The domain stayed clean. No aggregate learned about HTTP; the entire change was inside the inbound adapter.
- Errors are documented like successes. Each operation's error responses are in the OpenAPI spec, so they render in the generated API Reference.
Harder
typeURIs are now a contract. Renaming a slug is a breaking change for any consumer switching on it, even though the URI resolves to nothing.- A status
switchand a problem table to keep in sync.statusForandproblemFor/problemCatalog()must stay aligned; they are deliberately written in the same order with the same groupings, but nothing mechanically enforces it. detailleaks internal error strings. It is currentlyerr.Error()verbatim. For an internal service that is a feature (fast debugging); for a public API it would need sanitising.- The
400/422distinction requires judgement per error. "Malformed" vs "well-formed but illegal" is a real line, and every new domain error has to be placed on the right side of it.