🔥 Firehook

Blog · api rest

REST API error handling: clear, consistent, and actionable

Design error responses that developers can act on: consistent schema, stable codes, field errors, and safe messages.

API gateway with warning glow, symbolizing error handling

Errors are part of the developer experience

Errors shape how fast developers can recover from failure. A vague message wastes time and creates support tickets.

A structured error response turns a failure into a clear action: fix a field, refresh a token, retry later.

Define one error schema

Pick a standard shape and use it everywhere. A minimal schema includes a stable code, a human message, and optional details.

Consistency makes client libraries easier to build and reduces the need for special cases.

Use stable error codes

Human messages can change, but error codes should be stable. Clients can use codes to decide whether to retry or show a specific prompt.

Keep codes short, descriptive, and documented.

Field-level validation

When a payload is invalid, return field-level errors. This lets clients highlight the exact input that needs fixing.

A typical format is `errors: [{ field: 'email', message: 'invalid' }]`.

Do not leak internal details

Never expose stack traces or internal exceptions. They can reveal vulnerabilities and confuse API consumers.

Log internal errors server-side with a correlation ID, then return a safe, friendly message.

Handle partial failures explicitly

Batch operations can partially succeed. Return a status that reflects partial success and include per-item results.

That lets clients retry only failed items instead of the entire batch.

Document your error catalog

A short catalog of codes and fixes saves time. It reduces support load and improves client quality.

The best error documentation is small, specific, and consistent.

FAQ

Should I use a standard error format?
Yes. A consistent error schema makes clients simpler and reduces debugging time.
Is it okay to expose internal error messages?
No. Use user-safe messages and log internal details separately.
Should validation errors be 400 or 422?
Both are used. Choose one and be consistent across the API.
How do I handle partial failures?
Return a clear status and include per-item errors in a structured array.
Do I need a unique error code?
Yes. Stable internal codes let clients handle errors programmatically.