Errors, Status Codes, and Contracts
Core Concepts (5)
- 1.Status codes tell clients whether to fix, retry, or give up
- 2.A structured error body beats a free-text message
- 3.Errors are part of the contract and need versioning too
- 4.Never leak internals through error messages
- 5.Correlation identifiers connect a client report to a server log
Map to Your Experience
Recall the last API error that wasted your time. What information would have let you fix it in one minute?
Design Brief: Design an Error Model
Learn
Errors are where an API shows its character. A client developer reading a failure needs to answer three questions quickly: was it my fault, can I try again, and what do I change. Status codes answer the first two. The 400 range means the request was wrong and repeating it unchanged will fail again. The 500 range means the server failed and a retry may succeed.
Within those ranges, be precise. Use 401 when identity is missing, 403 when identity is known but not allowed, 404 when the resource does not exist, 409 for a conflict with current state, and 422 for a well-formed request that fails validation. Use 429 when a client exceeds a rate limit and include a hint about when to return. Avoid returning 200 with an error inside the body, because every generic client and proxy will misread it.
Pair the code with a structured body. A common shape has a stable machine-readable error code, a human message, a pointer to the offending field, and a correlation identifier. The machine code is the contract: clients branch on it, so it must be documented and never renamed. The human message may change freely.
Be careful about what you reveal. Stack traces, SQL fragments, and internal host names help attackers, not users. Log the details on the server, keyed by the correlation identifier, and return only what the caller can act on. Authentication failures should usually be vague about which part was wrong.
Finally, treat errors as part of the versioned surface. Adding a new error code can break a client that assumed a closed set, so document that clients must handle unknown codes gracefully. A short guide that lists every code with its cause and remedy is often the most valuable page in the documentation.
Map
Take a flow you know, such as checkout. List six things that can go wrong and assign each a status code, a machine error code, and a verdict of retry, fix, or give up. Note which ones leak too much if you echo the cause.
Design
Write the error model for the payments API: the body shape, the table of codes, and the retry guidance. Add a short note on how a support engineer would use the correlation identifier to find the cause in logs.
Requirements
- •Choose status codes for at least 6 failure cases
- •Define a single error body shape
- •Mark which errors are retryable
Constraints
- •Messages must not reveal internal systems
- •Clients in several languages
Must Cover
Trade-off Axis
detail vs safety
Referenced Patterns
Competencies Advanced
Ready to Design?
Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.
Open in Studio →