Files
atelier/domains/errors/patterns.md
T
Jon Chery 496303471d docs(milestone): complete v0.1 — initial framework
---ci---
project: atelier
phase: 7
milestone: v0.1
status: complete
phase_role: final
milestone_complete: true
requirements:
  covered: [ATELIER-01, ATELIER-02, ATELIER-03, ATELIER-04, ATELIER-05, ATELIER-06, ATELIER-07, ATELIER-08, ATELIER-09, ATELIER-10, ATELIER-11, ATELIER-12, ATELIER-13, ATELIER-14, ATELIER-15, ATELIER-16, ATELIER-17, ATELIER-18, ATELIER-19, ATELIER-20, ATELIER-21, ATELIER-22, ATELIER-23, ATELIER-24, ATELIER-25, ATELIER-26, ATELIER-27, ATELIER-28, ATELIER-29, ATELIER-30, ATELIER-31, ATELIER-32, ATELIER-33, ATELIER-34, ATELIER-35]
  partial: []
ship:
  milestone: v0.1
  type: NFR
  tag: v0.0.7
  merge: milestone/v0.1-atelier -> main
  release: https://git.cloudinit.dev/cloudinit-bot/atelier/releases/tag/v0.0.7
---/ci---

Milestone v0.1 — Initial Framework (NFR, complete).
8 core principles (C1-C8), 11 domains, 110 domain principles, 27 derived docs, 4 good + 3 bad examples, 4 language docs, full matrix, 3 review docs.
All 35 requirements covered. 7 patches (v0.0.0 pre-execution through v0.0.7 final). v0.0.7 IS the v0.1.0 milestone release.
2026-08-05 00:36:55 +00:00

94 lines
3.2 KiB
Markdown

# Error Patterns — Derived Rules
> Derives from `domains/errors/first-principles.md`. Common error-handling patterns and when to use them.
## Pattern 1: Result Type (P1 Errors are Data)
```typescript
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
function divide(a, b): Result<number, string> {
if (b === 0) return { ok: false, error: "division by zero" };
return { ok: true, value: a / b };
}
```
- Errors are values, not exceptions. The caller handles them explicitly.
- Use when errors are expected (parsing, validation, fallible operations).
- Avoid when errors are truly exceptional (out of memory, programmer error) — use exceptions/panics.
## Pattern 2: Sentinel Error (P4 Preserve Context)
```go
var ErrNotFound = errors.New("not found")
if errors.Is(err, ErrNotFound) { ... }
```
- A sentinel is a known error value the caller checks against.
- Use for a small, known set of error conditions.
- Wrap with context: `fmt.Errorf("load user %d: %w", id, ErrNotFound)`.
## Pattern 3: Typed Error (P1, P3 Fail Specifically)
```rust
enum AppError {
NotFound(String),
Invalid(String),
Internal(String),
}
```
- A typed error carries the kind and the detail.
- The caller matches on kind; the detail is for logging/display.
- Use when there are distinct error categories the caller handles differently.
## Pattern 4: Error Wrapping (P4 Preserve Context)
```go
return fmt.Errorf("query users: %w", err)
```
- Wrap errors as they cross boundaries. The outer error says "what was happening"; the inner says "what went wrong."
- The error chain is the stack trace of intent. Read it top-down: "I was doing X, which failed because Y, which was caused by Z."
- Never wrap with a generic message ("operation failed"). Wrap with the specific operation.
## Pattern 5: Fail Fast (P6 Unrecoverable Means Stop)
```typescript
if (config.secret === undefined) throw new Error("config.secret is required");
```
- For unrecoverable conditions, fail immediately. Do not limp on.
- Use at startup: missing required config, missing database, missing secrets.
- Do not use for recoverable conditions (a 404 is recoverable; a missing secret is not).
## Pattern 6: Retry with Backoff (P5 Recoverable When Possible)
```python
for attempt in range(3):
try:
return do_thing()
except TransientError:
sleep(2 ** attempt)
raise PermanentError()
```
- Retry transient errors (network, 429, 5xx). Do not retry permanent errors (400, 401).
- Exponential backoff with jitter. A retry storm is worse than the original failure.
- Bounded retries. Infinite retry is infinite hang (P8 Timeout Discipline).
## Pattern 7: Circuit Breaker (P3 Defense in Depth via errors P7)
- After N consecutive failures, stop trying. Return a fallback or error immediately.
- Use for external dependencies (a downstream service, an API).
- The breaker resets after a cooldown. Protects the system and the downstream.
## What Violates Error Patterns
| Violation | Pattern |
|-----------|---------|
| `catch (e) { return null }` | (anti-pattern, P2 Fail Loudly) |
| `throw new Error("error")` | P3 Fail Specifically |
| Retry without backoff | P5, P8 |
| `return null` for "not found" | P1 (errors are data, not absence) |
| `throw` in a recovery path | P6 (fail fast in the wrong place) |