MediumπŸ‘€ 3-5 yearsπŸ‘€ 8-15 years 1 min read

How do you design consistent, useful API error responses?

Asked inAmazonMicrosoftDeloitteCognizant
#error handling#problem details#rfc 7807#status codes#api design
Report issue

⚑ 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

Status code→right 4xx/5xx
Body (RFC 7807)β†’type/title/status/detail
Add→stable error code + field errors + traceId
Never→stack traces / internal details

⌨️Hands-on Keyboard

Learn by Doing

json
{
  "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:

βœ“ correct status codesβœ“ RFC 7807 / consistent bodyβœ“ stable error codesβœ“ field-level validation errorsβœ“ correlation id, no stack traces

⚠️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

RFC 7807 Problem Details@ControllerAdviceOpenTelemetry

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?

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.

Related Questions