error-handling-patterns
Robust error handling patterns for TypeScript applications
What this skill does
# Error Handling Patterns
## Core Principles
1. **Fail fast** - Detect and report errors early
2. **Fail safely** - Errors shouldn't crash the application
3. **Fail informatively** - Provide actionable error messages
4. **Recover gracefully** - Handle expected failures
## Custom Error Classes
```typescript
// Base application error
export class AppError extends Error {
constructor(
message: string,
public readonly code: string,
public readonly statusCode: number = 500,
public readonly details?: unknown
) {
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
toJSON() {
return {
code: this.code,
message: this.message,
...(this.details && { details: this.details }),
};
}
}
// Specific error types
export class ValidationError extends AppError {
constructor(message: string, details?: Record<string, string>) {
super(message, 'VALIDATION_ERROR', 400, details);
}
}
export class NotFoundError extends AppError {
constructor(resource: string, id: string) {
super(`${resource} with id '${id}' not found`, 'NOT_FOUND', 404);
}
}
export class UnauthorizedError extends AppError {
constructor(message = 'Authentication required') {
super(message, 'UNAUTHORIZED', 401);
}
}
export class ForbiddenError extends AppError {
constructor(message = 'Insufficient permissions') {
super(message, 'FORBIDDEN', 403);
}
}
```
## Result Type Pattern
```typescript
type Result<T, E = Error> =
| { success: true; data: T }
| { success: false; error: E };
// Helper functions
function ok<T>(data: T): Result<T, never> {
return { success: true, data };
}
function err<E>(error: E): Result<never, E> {
return { success: false, error };
}
// Usage
async function findUser(id: string): Promise<Result<User, NotFoundError>> {
const user = await db.users.findUnique({ where: { id } });
if (!user) {
return err(new NotFoundError('User', id));
}
return ok(user);
}
// Consuming
const result = await findUser('123');
if (!result.success) {
console.error(result.error.message);
return;
}
const user = result.data; // Type-safe User
```
## Error Boundaries (React)
```typescript
import { Component, ErrorInfo, ReactNode } from 'react';
interface Props {
children: ReactNode;
fallback?: ReactNode;
onError?: (error: Error, info: ErrorInfo) => void;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<Props, State> {
state: State = { hasError: false, error: null };
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, info: ErrorInfo) {
this.props.onError?.(error, info);
}
render() {
if (this.state.hasError) {
return this.props.fallback ?? (
<div role="alert">
<h2>Something went wrong</h2>
<pre>{this.state.error?.message}</pre>
</div>
);
}
return this.props.children;
}
}
```
## Async Error Handling
```typescript
// Wrapper for async functions
function tryCatch<T>(
promise: Promise<T>
): Promise<[null, T] | [Error, null]> {
return promise
.then((data) => [null, data] as [null, T])
.catch((error) => [error as Error, null]);
}
// Usage
const [error, user] = await tryCatch(fetchUser(id));
if (error) {
handleError(error);
return;
}
// user is guaranteed to be defined here
// Multiple operations
async function processOrder(orderId: string) {
const [orderError, order] = await tryCatch(getOrder(orderId));
if (orderError) return err(orderError);
const [paymentError] = await tryCatch(processPayment(order));
if (paymentError) {
await tryCatch(rollbackOrder(order));
return err(paymentError);
}
return ok(order);
}
```
## Retry Pattern
```typescript
interface RetryOptions {
maxAttempts: number;
delayMs: number;
backoff?: 'linear' | 'exponential';
shouldRetry?: (error: Error) => boolean;
}
async function withRetry<T>(
fn: () => Promise<T>,
options: RetryOptions
): Promise<T> {
const { maxAttempts, delayMs, backoff = 'exponential', shouldRetry } = options;
let lastError: Error;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error as Error;
if (shouldRetry && !shouldRetry(lastError)) {
throw lastError;
}
if (attempt < maxAttempts) {
const delay = backoff === 'exponential'
? delayMs * Math.pow(2, attempt - 1)
: delayMs * attempt;
await sleep(delay);
}
}
}
throw lastError!;
}
// Usage
const data = await withRetry(() => fetchFromAPI(), {
maxAttempts: 3,
delayMs: 1000,
shouldRetry: (err) => err.message.includes('timeout'),
});
```
## Validation Errors
```typescript
import { z } from 'zod';
function validateInput<T>(schema: z.ZodSchema<T>, input: unknown): Result<T, ValidationError> {
const result = schema.safeParse(input);
if (!result.success) {
const details = result.error.flatten().fieldErrors;
return err(new ValidationError('Invalid input', details));
}
return ok(result.data);
}
// Usage
const userSchema = z.object({
email: z.string().email(),
age: z.number().min(18),
});
const result = validateInput(userSchema, req.body);
if (!result.success) {
return res.status(400).json(result.error.toJSON());
}
```
## Logging Errors
```typescript
interface ErrorContext {
userId?: string;
requestId?: string;
path?: string;
[key: string]: unknown;
}
function logError(error: Error, context: ErrorContext = {}) {
const payload = {
timestamp: new Date().toISOString(),
name: error.name,
message: error.message,
stack: error.stack,
...(error instanceof AppError && { code: error.code }),
...context,
};
console.error(JSON.stringify(payload));
// Send to error tracking service
if (process.env.NODE_ENV === 'production') {
// Sentry.captureException(error, { extra: context });
}
}
```
## Best Practices
1. **Create specific error types** for different failure modes
2. **Use Result types** for expected failures (validation, not found)
3. **Throw errors** for unexpected failures (bugs, system errors)
4. **Include context** in error messages for debugging
5. **Log at boundaries** - API handlers, event processors
6. **Validate at boundaries** - external input, API responses
7. **Implement retry** for transient failures
8. **Use error boundaries** in React for graceful degradation
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.