Loading...
Loading...
Design error handling strategies for TypeScript and Python applications — exception hierarchies, Result/Either types, retry patterns, error boundaries, and structured error logging. Use when designing error handling architecture, choosing between exceptions and Result types, implementing retry logic, or building error recovery flows. Activate on "error handling", "exception hierarchy", "Result type", "retry pattern", "circuit breaker", "error boundary", "Pokemon exception". NOT for debugging specific runtime errors, logging infrastructure setup, or monitoring/alerting configuration.
npx skill4agent add erichowens/some_claude_skills error-handling-patterns__cause____context__flowchart TD
Q1{Is this a programming error\nor contract violation?} -->|Yes| EX[Throw exception\nlet it crash]
Q1 -->|No| Q2{Is the error part of\nnormal control flow?}
Q2 -->|Yes| Q3{What is the call site context?}
Q2 -->|No| Q4{Do callers need to\ndistinguish error types?}
Q3 -->|Functional / monad-friendly| RT[Result or Either type]
Q3 -->|Simple script or CLI| EC[Error code + message]
Q4 -->|Yes| EH[Typed exception hierarchy]
Q4 -->|No| GE[Generic exception\nwith structured message]
EX --> NOTE1[Never catch at boundary —\nlet process restart]
RT --> NOTE2[Compose with map/flatMap;\ncheck references/error-hierarchy-examples.md]
EH --> NOTE3[See hierarchy design rules below]| Transient (retry may succeed) | Permanent (retry won't help) | |
|---|---|---|
| User-actionable | Rate limit, quota exceeded | Invalid input, unauthorized |
| System-actionable | Network timeout, DB connection | Data corruption, schema mismatch |
flowchart TD
E[Error occurs] --> C1{Is error transient?\nTimeout, 429, 503, connection reset}
C1 -->|No| FAIL[Fail immediately\nReturn error to caller]
C1 -->|Yes| C2{Have we exceeded\nmax retry attempts?}
C2 -->|Yes| DLQ[Send to dead letter queue\nor return final failure]
C2 -->|No| C3{Is circuit breaker OPEN?}
C3 -->|Yes| CB[Return circuit-open error\nDo not attempt request]
C3 -->|No| WAIT[Wait: exponential backoff\n+ full jitter]
WAIT --> RETRY[Retry request]
RETRY --> C1
CB --> PROBE{After timeout:\nsend probe request}
PROBE -->|Success| CLOSE[Close circuit\nResume normal traffic]
PROBE -->|Fail| CBreferences/retry-patterns.md// Base application error — all domain errors extend this
class AppError extends Error {
readonly code: string;
readonly statusCode: number;
readonly isOperational: boolean; // false = programmer error, crash process
constructor(message: string, code: string, statusCode: number, isOperational = true) {
super(message);
this.name = this.constructor.name;
this.code = code;
this.statusCode = statusCode;
this.isOperational = isOperational;
Error.captureStackTrace(this, this.constructor);
}
}
// Domain-specific errors
class ValidationError extends AppError {
readonly fields: Record<string, string[]>;
constructor(fields: Record<string, string[]>) {
super('Validation failed', 'VALIDATION_ERROR', 422);
this.fields = fields;
}
}
class NotFoundError extends AppError {
constructor(resource: string, id: string) {
super(`${resource} ${id} not found`, 'NOT_FOUND', 404);
}
}
class RateLimitError extends AppError {
readonly retryAfterMs: number;
constructor(retryAfterMs: number) {
super('Rate limit exceeded', 'RATE_LIMIT', 429);
this.retryAfterMs = retryAfterMs;
}
}references/error-hierarchy-examples.mdtype Result<T, E = AppError> =
| { ok: true; value: T }
| { ok: false; error: E };
// Helpers
const ok = <T>(value: T): Result<T, never> => ({ ok: true, value });
const err = <E>(error: E): Result<never, E> => ({ ok: false, error });
// Usage — caller is forced to handle both cases
async function fetchUser(id: string): Promise<Result<User, NotFoundError | NetworkError>> {
try {
const user = await db.users.findById(id);
if (!user) return err(new NotFoundError('User', id));
return ok(user);
} catch (e) {
return err(new NetworkError('DB unavailable', { cause: e }));
}
}
// At call site — no silent failures
const result = await fetchUser(userId);
if (!result.ok) {
if (result.error instanceof NotFoundError) return res.status(404).json(...);
return res.status(500).json(...);
}
const user = result.value; // typed, safeclass RouteErrorBoundary extends React.Component<Props, State> {
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, info: React.ErrorInfo) {
// Log to error tracking, not console.error in production
logger.error('Render error', { error, componentStack: info.componentStack });
}
render() {
if (this.state.hasError) {
return <ErrorFallback error={this.state.error} onRetry={this.reset} />;
}
return this.props.children;
}
}raise X from Yclass AppError(Exception):
"""Base error. All domain errors subclass this."""
def __init__(self, message: str, code: str, status: int = 500):
super().__init__(message)
self.code = code
self.status = status
class DatabaseError(AppError):
def __init__(self, operation: str, cause: Exception):
super().__init__(f"DB error during {operation}", "DB_ERROR", 503)
self.__cause__ = cause # explicit chain
# In application code
try:
result = db.execute(query)
except psycopg2.OperationalError as e:
raise DatabaseError("user_fetch", e) from e # preserves full traceback// Good: structured, queryable, developer-oriented
logger.error('Payment processing failed', {
error: {
code: error.code,
message: error.message,
stack: error.stack,
},
context: {
userId,
orderId,
amount,
paymentProvider,
attempt: retryCount,
},
correlation: { requestId, traceId },
});
// Then surface a sanitized message to the user
// NEVER leak error.message to users — it may contain internals
return res.status(500).json({
error: 'Payment could not be processed. Please try again.',
errorId: requestId, // so support can look it up
});try/catch// Wrong — swallows everything including programming errors
try {
await processOrder(order);
} catch (e) {
console.error('something went wrong', e); // lost forever
}
// Right — catch only what you can handle, let the rest propagate
try {
await processOrder(order);
} catch (e) {
if (e instanceof RateLimitError) {
await queue.requeue(order, { delay: e.retryAfterMs });
return;
}
// programming errors, unexpected DB errors — let them crash
throw e;
}catch (e) { }catch (e) { log(e) }except Exception as e: passthrow new Error('NOT_FOUND: User 123')// Wrong — caller must parse strings, breaks silently on rename
throw new Error(`RATE_LIMIT: retry after ${ms}ms`);
// Caller: if (error.message.startsWith('RATE_LIMIT')) { ... }
// Right — typed, refactor-safe, IDE-navigable
throw new RateLimitError(ms);
// Caller: if (error instanceof RateLimitError) { ... error.retryAfterMs ... }# Wrong
raise Exception(f"rate_limit:{retry_after}")
# Right
raise RateLimitError(retry_after_ms=retry_after)instanceof Error.startsWith().includes()messagereferences/retry-patterns.mdreferences/error-hierarchy-examples.md