{"success":true,"name":"version-and-shape-node-js-api-errors-as-part-of-the-contract","description":"Designing how a Node.js API reports errors to clients.","category":"ai-brimind","path":"ai-brimind/version-and-shape-node-js-api-errors-as-part-of-the-contract/SKILL.md","file":"SKILL.md","content":"---\nname: version-and-shape-node-js-api-errors-as-part-of-the-contract\ndescription: \"Designing how a Node.js API reports errors to clients.\"\n---\n\n# Version and Shape Node.js API Errors as Part of the Contract\n\n## When to use\nProject is a Node.js API, and you are deciding error response formats, status codes, or how handlers report failures.\n\n## Example request\nEvery failure returns 500 with err.message → clients can't react differently to 'not found' vs 'server bug', and internals leak — status codes are the contract. Error shapes differ per endpoint → clients write per-endpoint parsing; one error envelope ({error: {code, message}}) owns the format. Stack traces sent to the client in production → internals exposed — log the stack, send a safe message and an id. Business rules signaled by throwing strings → custom error classes with status codes let one middleware map errors to responses.\n\n## Instructions\nAct as an API designer who treats error responses as a published contract clients will code against. Before writing handlers, define once: (1) a single error envelope shape (e.g. {error: {code, message, details?}}) used by EVERY endpoint; (2) the status code policy — 400 malformed input, 401 unauthenticated, 403 forbidden, 404 missing, 409 conflict, 500 only for genuine server bugs; (3) custom error classes (or an http-errors package) carrying the status, so business code throws typed errors and ONE error middleware maps them to responses — no per-route formatting. Never send stack traces or internal messages to clients in production: log the full error server-side with a correlation id, return the safe envelope with that id. Distinguish expected failures (validation, not-found — normal flow, warn-level at most) from unexpected ones (bugs — error-level, alerting). Only finalize once every endpoint provably flows through the shared error path, and explain how a client distinguishes each failure category from the response alone.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #992\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}