---
name: ecc-error-handling
description: Patterns for robust error handling across TypeScript, Python, and Go — typed errors, error boundaries, retries, circuit breakers, and user-facing error messages.
---

# Error Handling

Robust error handling patterns across languages.

## When to Activate
- Adding error handling to new code
- Reviewing error handling in PRs
- Designing error types
- Implementing retry/fallback logic

## Core Principles
1. **Fail fast** — detect errors early
2. **Never swallow** — every error is either handled or logged
3. **Types over strings** — typed errors over string messages
4. **User-facing clarity** — actionable messages, not stack traces
5. **Graceful degradation** — partial results > total failure

## Retry Pattern
```python
import time
from functools import wraps

def retry(max_attempts=3, delay=1, backoff=2):
    def decorator(fn):
        @wraps(fn)
        def wrapper(*args, **kwargs):
            last_exc = None
            for attempt in range(max_attempts):
                try:
                    return fn(*args, **kwargs)
                except (ConnectionError, TimeoutError) as e:
                    last_exc = e
                    time.sleep(delay * (backoff ** attempt))
            raise last_exc
        return wrapper
    return decorator
```

## Circuit Breaker
- **Closed**: requests pass through
- **Open**: requests fail fast after N failures
- **Half-open**: test request after timeout

## User-Facing Errors
- Show clear, non-technical messages
- Include actionable steps
- Log full details server-side
- Never expose stack traces to users

## Pitfalls
- ❌ Swallowing exceptions (`except: pass`)
- ❌ Exposing internal errors to users
- ❌ No distinction between recoverable and fatal errors
- ❌ Infinite retries without backoff
