Error Handling Patterns
Common patterns, error types, best practices, and monitoring tools.
Handling Patterns
Try-Catch: Wrap risky operations, handle exceptions locally
try { op() } catch(e) { handle(e) }
Error Boundary: React component catches render errors
<ErrorBoundary fallback={<Error />}><App /></ErrorBoundary>
Global Handler: Catch unhandled errors at app level
window.onerror, process.on("uncaughtException")
Promise.catch: Handle async errors in promises
fetch(url).catch(err => log(err))
Async/Await Try: Try-catch with async functions
try { await op() } catch(e) {}
Validation First: Check inputs before operations
if (!valid(input)) return error
Error Types & Fixes
Syntax Error: Code parsing fails
Fix: Check syntax, lint code
Runtime Error: Execution fails (null, undefined)
Fix: Null checks, type guards
Network Error: API fails, timeout
Fix: Retry logic, fallback data
Logic Error: Wrong algorithm/logic
Fix: Unit tests, code review
Type Error: Wrong type operation
Fix: TypeScript, validation
Resource Error: OOM, stack overflow
Fix: Optimize memory, limit recursion
Best Practices
Specific errors
Create custom error types for clarity
Error context
Include relevant data in error object
Graceful degradation
Show fallback UI, partial functionality
User messaging
Clear error messages, action suggestions
Logging
Log errors with stack trace, context
Monitoring
Track error rates, alert on spikes
Retry logic
Retry transient failures with backoff
Cleanup
Release resources in error handlers
Error Monitoring Tools
Sentry: Error tracking, performance monitoring
LogRocket: Session replay, error logs
Bugsnag: Error monitoring, release tracking
Rollbar: Real-time error tracking
Error Handling Checklist
1. Identify error sources. 2. Choose appropriate pattern. 3. Add context to errors. 4. Log with stack trace. 5. Show user-friendly message. 6. Provide recovery action. 7. Monitor error rates. 8. Set up alerts. 9. Review logs regularly. 10. Fix root causes. Good error handling = resilient app + happy users.