Error Handling — Notes#
Rules of thumb#
- Surface errors at API boundaries. Internal helpers can throw; public APIs return typed errors.
- Distinguish recoverable from bug.
NullPointerException≠ "card declined". - Preserve causes. Log + wrap with context, but don't lose the original.
- One mapper at the edge. Map domain errors to HTTP/gRPC statuses in one place.
- No silent retries. Every retry decision is logged and observable.
Naming#
NotFoundException,ValidationException,ConflictException— domain layer.OrderRepositoryException— infra layer.IllegalStateException,IllegalArgumentException— bug indicators.
Test what you ignore#
For every error path, write at least one negative test. The hidden bugs hide there.
Refs#
- Effective Java — Items 70–77 (exceptions).
- Rust book — "Error handling" chapter.
- Joe Duffy: "The Error Model" (Midori retrospective).
- Joel Spolsky: "Exceptions are bad" — read with an open mind.