Error Handling Standards
**Priority: P1 (HIGH)**
Error Architecture
- API Layer: Map domain errors to HTTP responses globally using the canonical error envelope.
- Domain Layer: Throw pure business errors. NO HTTP status codes here.
- Infra Layer: Wrap 3rd-party exceptions. NOT leak raw DB errors to API.
- Standard Shape: APIs return
{ "error": { "code", "message", "traceId", "details" } }.
See implementation examples for the standard error response shape.
Error Mechanics
- Wrap: Add context that helps the caller; preserve the cause where callers must match it (
fmt.Errorf("process: %w", err),new Error('msg', { cause })). Direct propagation or boundary translation is valid when wrapping exposes internals or adds nothing. - Log Once: Choose one owner per failure—the layer that handles or translates the request/job/CLI. Log there; layers beneath propagate without logging. Log-and-rethrow is duplicate when the next boundary logs again.
- Replace: Only when original error leaks sensitive details.
- Error Codes: Use
SCREAMING_SNAKE_CASEIDs (ORDER_PAYMENT_FAILED).
Anti-Patterns
- Swallowing Errors: Never
catch(e) {}without logging or re-throwing. - Never silently ignore an error: an empty catch must become an explicit log, handling branch, returned error, or rethrow.
- Double Logging: Do not log a failure in a helper and again at the boundary, nor log-and-rethrow into a boundary that logs again.
- Stack Traces: Never expose stack traces in API responses.
- Generic 500s: Use
400with specific details for validation instead of 500.
References
Failure-handling checklist
- Never swallow errors: do not use an empty
catchor silently ignore a failure. Log once at the owner, wrap or propagate beneath it, or map it deliberately at the correct boundary.