An API error should help a client decide what to do next. A human-readable sentence alone is fragile: clients should branch on a stable code or status, while people should receive a safe explanation.
Separate machine and human information
Use a consistent response shape with an error code, message, and optional field-level details. Keep the code stable across wording edits. For validation errors, identify the field and the expected constraint without echoing secrets or the entire submitted payload. Include a request ID that support staff can use to find server logs.
Choose status codes according to the condition: invalid input, unauthenticated, forbidden, missing resource, conflict, rate limit, or temporary server failure. Do not return a generic success status with an error in the body. For a retryable failure, say whether retry is safe and provide
Retry-AfterProtect internal details
Do not expose SQL errors, stack traces, filesystem paths, internal hostnames, or provider credentials. Log diagnostic detail on the server with sensitive values redacted. Map known dependency failures to deliberate public errors and avoid returning raw upstream response bodies.
Test error responses as part of the API contract, including malformed JSON, validation failures, authorization, rate limits, and unexpected exceptions. The best message is specific enough to support recovery and restrained enough to protect the system.
Give clients a documented recovery path
RFC 9457 defines a JSON problem-details format with fields such as type, title, status, detail, and instance. A stable problem type can help clients branch on a condition while the detail can explain a specific occurrence. Use the standard format consistently if you choose it. A legacy error envelope can also work when its documented machine-readable code and HTTP status agree. RFC 9457 Problem Details
An article save based on an older revision is a useful case for HTTP 409 Conflict. Give the editor enough information to keep unsaved text and guide the writer through reloading. Avoid returning another user's draft or internal database errors as debugging detail. Log a request identifier server-side and put that same identifier in a supportable response field where appropriate. RFC 9110 Conflict
Worked example
Return a conflict status for this failure and preserve the current editor state. Avoid exposing database queries, credentials, or another author’s content in error details.
json{ "success": false, "error": { "code": "REVISION_CONFLICT", "message": "This draft changed in another session. Reload or recover your local writing." } }




Community
Discussion
Loading comments…