How do you design consistent, useful API error responses?
β‘ Short Answer
Use correct HTTP status codes plus a consistent machine-readable error body (RFC 7807 Problem Details: type, title, status, detail, instance, plus a stable error code and field-level validation errors). Never leak stack traces; include a correlation id for support.
βCoffee Chat Question
Concept Made Simple
βHow do you design consistent, useful API error responses?β
π§ Mind Map Answer
Remember It Faster
β¨οΈHands-on Keyboard
Learn by Doing
{
"type": "https://api.x.com/errors/validation",
"title": "Validation failed",
"status": 400,
"code": "ORDER_INVALID",
"errors": [{ "field": "amount", "message": "must be > 0" }],
"traceId": "abc-123"
}π₯What If?
Think Beyond the Expected
Why is a stable machine-readable 'error code' better than relying on the HTTP status alone?
Many distinct failures map to the same status (400 covers dozens of validation errors). A stable code (ORDER_INVALID) lets clients branch logic and lets you change wording without breaking them β the status says the category, the code says the exact reason.
πReal World
Inconsistent error shapes across endpoints are a top API-consumer complaint; standardizing on RFC 7807 + stable error codes + a correlation id (for log lookup) is the enterprise norm.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βLeaking stack traces / internal messages
- βInconsistent error shapes per endpoint
- βUsing 200 with an error in the body
β Best Practices
- βAdopt RFC 7807 + stable error codes
- βCentralize mapping (@ControllerAdvice)
- βAlways include a traceId; never leak internals
πFollow-up Questions
- 1How do field-level validation errors look?
- 2Why include a correlation/trace id?
- 3Where do you centralize error mapping (e.g. @ControllerAdvice)?
π§©Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Status Codes (REST APIs) Interview question: "How do you design consistent, useful API error responses?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.