When to activate
- Designing error response format for new APIs
- Standardizing error codes across microservices
- Creating error documentation for API consumers
- Implementing global error handling middleware
- Mapping domain errors to HTTP status codes
When NOT to use
- For client-side error display/UI
- For logging and observability setup
- For exception tracking tool integration
Instructions
- Define error schema. Standard format:
{ error: { code, message, details, requestId, docsUrl } }.
- Map HTTP status codes. 400=validation, 401=auth, 403=permission, 404=not found, 409=conflict, 422=unprocessable, 429=rate limit, 500=server error.
- Create error code catalog. Machine-readable codes:
VALIDATION_ERROR, RESOURCE_NOT_FOUND, RATE_LIMIT_EXCEEDED, etc.
- Design validation errors. Return field-level errors:
{ field: "email", message: "must be valid email", code: "INVALID_FORMAT" }.
- Add request context. Include
requestId for debugging, timestamp, and path in every error response.
- Link to documentation. Include
docsUrl pointing to relevant API docs for self-service resolution.
- Implement middleware. Global error handler that catches exceptions and formats to standard schema.
Example
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Request body failed validation",
"details": [
{ "field": "email", "message": "must be a valid email address", "code": "INVALID_FORMAT" },
{ "field": "age", "message": "must be between 18 and 120", "code": "OUT_OF_RANGE" }
],
"requestId": "req_abc123",
"docsUrl": "https://api.example.com/docs/errors#VALIDATION_ERROR"
}
}
1---2name: error-handler-designer3description: Design consistent, actionable error response schemas with proper HTTP status codes and error categorization4---56## When to activate78- Designing error response format for new APIs9- Standardizing error codes across microservices10- Creating error documentation for API consumers11- Implementing global error handling middleware12- Mapping domain errors to HTTP status codes1314## When NOT to use1516- For client-side error display/UI17- For logging and observability setup18- For exception tracking tool integration1920## Instructions21221. **Define error schema.** Standard format: `{ error: { code, message, details, requestId, docsUrl } }`.232. **Map HTTP status codes.** 400=validation, 401=auth, 403=permission, 404=not found, 409=conflict, 422=unprocessable, 429=rate limit, 500=server error.243. **Create error code catalog.** Machine-readable codes: `VALIDATION_ERROR`, `RESOURCE_NOT_FOUND`, `RATE_LIMIT_EXCEEDED`, etc.254. **Design validation errors.** Return field-level errors: `{ field: "email", message: "must be valid email", code: "INVALID_FORMAT" }`.265. **Add request context.** Include `requestId` for debugging, `timestamp`, and `path` in every error response.276. **Link to documentation.** Include `docsUrl` pointing to relevant API docs for self-service resolution.287. **Implement middleware.** Global error handler that catches exceptions and formats to standard schema.2930## Example3132```json33{34 "error": {35 "code": "VALIDATION_ERROR",36 "message": "Request body failed validation",37 "details": [38 { "field": "email", "message": "must be a valid email address", "code": "INVALID_FORMAT" },39 { "field": "age", "message": "must be between 18 and 120", "code": "OUT_OF_RANGE" }40 ],41 "requestId": "req_abc123",42 "docsUrl": "https://api.example.com/docs/errors#VALIDATION_ERROR"43 }44}45```