Error responses (RFC 9457 problem details)
Give every error the same machine-readable shape so clients can branch on a stable
type, show a safe title/detail, and log an instance you can trace.
Paths such as scripts/... and references/... are relative to this skill's folder (${CLAUDE_SKILL_DIR} in Claude Code). Run scripts from the user's project with that folder's path.
When to use
- Defining the error format for a new API, or unifying inconsistent errors.
- Implementing a global error handler / middleware.
- Reviewing captured error responses or the error section of an OpenAPI spec.
When not to use
- GraphQL (errors live in the
errorsarray of the response) or gRPC (status codes andgoogle.rpc.Status). - Choosing status codes in general: see
rest-api-design.
Workflow
-
Inventory current errors. Grep the handlers for how errors are returned (
res.status(,HTTPException,abort(,response()->json() and collect sample bodies. Note leaks: stack traces, SQL, file paths, internal hostnames. -
Define the problem type catalog with references/problem-details.md: one row per error your clients must handle differently (
validation-error,insufficient-funds,rate-limited). Each gets a stabletypeURI, a fixedtitle, a status, and any extension members. Useabout:blankonly when the status code says everything. -
Pick the validation error shape:
errors: [{"detail": ..., "pointer": "#/items/0/qty"}](JSON Pointer, RFC 6901) as in RFC 9457's own examples. -
Implement one central handler using the matching snippet in references/implementations.md (Express, FastAPI, Flask, Laravel, plain PHP). Rules: set
Content-Type: application/problem+json; the bodystatusequals the HTTP status; unknown exceptions become a generic 500 with a correlation ID and are logged server-side with the stack trace. -
Document it in OpenAPI: a shared
components.schemas.Problemandcomponents.responses(BadRequest,NotFound,Conflict,TooManyRequests) withapplication/problem+jsonexamples. Template: assets/problem-components.yaml. -
Lint captured bodies and the spec:
python scripts/problem_lint.py doc error-samples/*.json --status 422 python scripts/problem_lint.py spec openapi.yaml --format jsonExit 1 means findings at or above
--fail-on(defaulterror). -
Test each problem type: status, content type,
typevalue, no internal details.
Rules
typeis the contract; clients branch on it. Never change it for an existing error.titleis fixed pertype; put the occurrence-specific text indetail.detailis for humans; do not make clients parse it.- Never include stack traces, SQL, exception class names or secrets. Log them with the
instance/trace ID instead. - Do not reveal whether a resource exists to callers who may not see it (return 404).
- Send
Retry-Afterwith 429 and 503. SendWWW-Authenticatewith 401.
Output
- A problem type catalog table:
type | title | status | when | extension members. - The handler code for the user's framework, wired in, with tests.
- OpenAPI components for the errors.
- Linter results before/after.
Example prompts
- "Make all our Express API errors consistent and stop leaking stack traces."
- "What should a validation error response look like?"
- "Add problem+json error handling to this FastAPI app."