{"success":true,"name":"define-one-error-contract-for-the-whole-api-then-speak-it-ev","description":"Building multiple API endpoints where each invents its own way to report errors","category":"ai-brimind","path":"ai-brimind/define-one-error-contract-for-the-whole-api-then-speak-it-ev/SKILL.md","file":"SKILL.md","content":"---\nname: define-one-error-contract-for-the-whole-api-then-speak-it-ev\ndescription: \"Building multiple API endpoints where each invents its own way to report errors\"\n---\n\n# Define One Error Contract for the Whole API, Then Speak It Everywhere\n\n## When to use\nThe project has (or will have) several endpoints built over time, and errors currently come back in different shapes — sometimes a string, sometimes { error }, sometimes { message }, sometimes just a status code — so the frontend cannot handle failures uniformly.\n\n## Example request\nOne endpoint returns { error: '...' }, another { message: '...' }, a third plain text → the frontend needs different parsing per screen and usually gives up; a single agreed error shape ends that. Validation failure returns 200 with { success: false } → generic error handling in the HTTP client never fires because 200 means 'fine'; status codes are part of the contract. A DB exception leaks its raw text and stack to the client → internals are exposed and the message helps nobody; the server should log details and return a clean, safe message. Every endpoint wraps its own try/catch differently → the fix has a name: one central error-handling middleware that formats ALL failures the same way.\n\n## Instructions\nAct as the architect of a shared language between backend and frontend: success has one grammar, and failure must have one too — otherwise every screen ends up bilingual in five dialects of 'something broke'. Do this: (1) Before writing more endpoints, define the error contract once and write it down: which HTTP status codes you use for what (400 bad input, 401 not logged in, 403 not allowed, 404 not found, 500 server fault) and one fixed JSON shape for every error body — e.g. a machine-readable code, a human message, and optionally the field at fault. The exact fields matter less than there being exactly ONE shape. (2) Enforce it in one place, not per endpoint: a central error-handling middleware (Express and similar frameworks have this built in — use the framework's mechanism, don't hand-wrap every route in its own try/catch formatting). Routes throw or forward errors; the middleware translates them into the contract. (3) Split every error into two audiences on purpose: the log gets the full truth (stack, query, IDs) for you at debugging time; the client gets the clean contract message — never raw exception text, which leaks internals and frightens users. (4) Never report failure inside a 200 — generic HTTP-layer handling on the frontend depends on honest status codes. (5) On the frontend, write ONE error interpreter against this contract (in the shared API layer, not per component) so 'show the message, mark the field' works identically on every screen. If the project already has endpoints in mixed styles, adopt the contract for everything new and migrate old ones as you touch them — say that plan out loud. Only proceed once you can show the written contract and point to the single middleware that guarantees it, then explain why any future endpoint gets uniform errors for free instead of by discipline.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #198\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}