Design an API error taxonomy: pair HTTP status with stable problem types
A useful API error taxonomy does not collapse every failure into one error; it helps the caller answer, “Who needs to do what next?” Use the HTTP status code for general protocol meaning and a stable identifier such as a Problem Details type for the API-specific problem. If clients branch on prose or internal exception names, a wording change or implementation replacement can break the contract.
A useful API error taxonomy does not collapse every failure into one error; it helps the caller answer, “Who needs to do what next?” Use the HTTP status code for general protocol meaning and a stable identifier such as a Problem Details type for the API-specific problem. If clients branch on prose or internal exception names, a wording change or implementation replacement can break the contract.
Start with the recovery boundary
Do not begin by enumerating every exception an API might produce. Define the boundaries that change the caller’s next action: should it fix the request, authenticate again, check permissions or resource state, retry later, or give an occurrence ID to support? This action-oriented grouping makes SDKs, interfaces, and operations guidance more consistent than a list of status codes alone.
| Problem category | Caller or operator action | HTTP meaning that may fit |
|---|---|---|
| Request syntax or value is invalid | Correct the request and send it again; provide a stable reason and field location when useful. | 400, or 422 under the API contract |
| Authentication is missing or invalid | Authenticate or refresh credentials. | 401 with an authentication challenge |
| Permission is insufficient | Check access rights; do not blindly retry with the same credentials. | 403 |
| Resource missing or state conflict | Check the URI, current state, or concurrent update; a person may need to decide what to do. | 404, 409, or 412 for a failed conditional request |
| Rate limit or temporary service unavailability | Follow any wait guidance and confirm duplicate safety if the operation outcome is uncertain. | 429, 503 |
| Gateway or upstream failure | Operators investigate the upstream path. The status alone does not prove the original operation was not applied. | 502 or 504, depending on the situation |
| Unexpected server failure | The caller keeps the occurrence identifier; operators investigate the cause. | 500 |
These statuses are examples, not an automatic mapping. Each HTTP status has defined semantics; do not first label something “a client error” and then choose any 4xx. For example, 404 can mean no current representation exists or the server is unwilling to disclose one, and it does not say whether the condition is temporary or permanent. 502 means a gateway received an invalid upstream response; 504 means a required upstream response did not arrive in time. (RFC 9110 §§15.5–15.6)
Separate general status from API-specific problem identity
If an API uses Problem Details, treat its type URI as the primary identifier for a problem type. For example, two different conditions may both use 409 Conflict: an order is already paid, or an inventory version changed. They can have distinct stable types if callers need different actions. Conversely, do not split types unnecessarily when the same response leads to the same recovery.
HTTP/1.1 409 Conflict
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/order-state-conflict",
"title": "Order state prevents this action",
"status": 409,
"detail": "The order has already been paid.",
"instance": "urn:problem:01JEXAMPLE"
}This synthetic response illustrates the format. In RFC 9457, type is the primary problem-type identifier, title is a short summary of that type, and detail explains this occurrence. detail is human-readable prose, not a code for machines to parse. A problem type can define extensions such as fields, links, or retry guidance, but clients should be designed to ignore extension members they do not know. Problem Details is optional: retain a suitable existing domain response format, or use only a status code when it conveys enough information. (RFC 9457 §§1, 3, 4)
Make clients resilient to taxonomy changes
**Choose stable identifiers.** Manage a `type` URI or documented extension code as a durable contract name. If the URI is intended to link to documentation later, own and maintain it so the identifier does not change with every new page version.
**Separate type from occurrence.** `type` identifies a recurring problem; `instance` and a request ID identify one occurrence. Do not use changing prose or per-occurrence IDs as the type identifier.
**Specify status and type together.** Document the type’s meaning, allowed status, caller action, safe retry conditions, and required extensions. Keep the actual HTTP status consistent with the body’s `status` member.
**Add new types compatibly.** Clients can handle known types specifically and fall back safely using the status and common fields for unknown types. Do not silently change the meaning of an existing type in a response that needs to remain backward compatible.
**Do not expose internal exceptions.** Map database-driver or framework exceptions, stack traces, and secrets to an allowlisted public type. Keep sensitive diagnostics in access-controlled logs linked by an occurrence identifier.
If types are too granular, clients must handle too many cases; if they are too broad, callers cannot choose the right recovery. Set boundaries by asking whether the caller’s or operator’s next action actually changes. Document status, type, and recovery together so error handling stays predictable as the API grows.
References
[RFC 9110: HTTP Semantics §§15.5–15.6](https://www.rfc-editor.org/rfc/rfc9110.html)
[RFC 9457: Problem Details for HTTP APIs §§1, 3, 4](https://www.rfc-editor.org/rfc/rfc9457.html)