Designing error responses with RFC 9457 problem details

Difficulty: Intermediate

Question

How would you design a consistent error response format for an API? What is RFC 9457 problem details?

Answer

Errors are the part of an API that developers see most when integrating, since success is quiet and failure is loud. A good error response is like a good hospital discharge note: it says what happened, where, whether it is your fault, and what to do next. A bad one is a bare 500 with the text 'something went wrong'.

The problem with ad hoc formats is that every team invents its own: one returns {error: 'msg'}, another {message, code}, a third {errors: []}. Clients then need custom parsing per endpoint. RFC 9457 (which obsoletes RFC 7807) standardises this as 'problem details', with the media type application/problem+json. It defines five standard members. type is a URI that identifies the problem category and ideally links to documentation. title is a short, human readable summary that should not change between occurrences. status echoes the HTTP status code. detail is a human readable explanation specific to this occurrence. instance is a URI identifying this specific occurrence, handy for support tickets and log correlation.

You are allowed, and encouraged, to add extension members. Typical ones are a stable machine readable code such as insufficient_funds, an errors array for field-level validation problems, a request_id or trace id, and for rate limits a retry_after value. Clients should switch on type or code, never parse the human message, since messages can be reworded or localised.

Field-level validation deserves care. Return all problems at once rather than one per round trip, and give each a JSON pointer or field name plus a reason. That lets a form highlight every invalid input immediately.

Security considerations: never include stack traces, SQL fragments, internal hostnames or library versions in production responses. For 500s, return a generic message and a request id, and log the details server side. Also be careful with authentication errors: 'invalid username or password' rather than revealing which one was wrong, to avoid account enumeration.

Consistency requirements: use the same envelope for every error from every endpoint, including those raised by frameworks, gateways and rate limiters. A very common gap is that your business code returns nice JSON, but the gateway returns an HTML error page on timeout. Make sure the edge produces problem+json too.

HTTP status and body must agree. Do not send 200 with an error object. Include the right headers too: WWW-Authenticate for 401, Retry-After for 429 and 503, Allow for 405.

Localisation: keep title and type stable and English for machines, and let clients localise from the code. If you must provide translated detail, honour Accept-Language.

Interview flow: explain the pain of inconsistent errors, introduce RFC 9457 and its five fields, add extensions for code and validation errors, and finish with the security and consistency points. Stripe's own format is a good real-world contrast, since it predates the RFC but follows the same spirit: a type, a code, a message and a param.

Code examples

Problem details response

HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json

{
  "type": "https://api.example.com/problems/validation-error",
  "title": "Your request parameters didn't validate",
  "status": 422,
  "detail": "2 fields failed validation",
  "instance": "/v1/orders/requests/req_8f2a",
  "code": "validation_error",
  "errors": [
    { "pointer": "/quantity", "reason": "must be >= 1" },
    { "pointer": "/address/pincode", "reason": "must be 6 digits" }
  ]
}

Standard members plus extensions for a stable code and per-field errors.

Express error middleware

app.use((err, req, res, next) => {
  const status = err.status ?? 500;
  res.status(status).type('application/problem+json').json({
    type: err.type ?? 'about:blank',
    title: status === 500 ? 'Internal Server Error' : err.title,
    status,
    detail: status === 500 ? undefined : err.detail,
    instance: req.id,
    ...(err.errors && { errors: err.errors })
  });
});

One place produces every error; 500s hide internal detail.

Key points

Common follow-up questions

Interview tips

Concepts covered

error handling, RFC 9457, problem details, validation errors

More API Design questions