You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error handling patterns across languages and layers — operational vs programmer errors, retry strategies, circuit breakers, error boundaries, HTTP responses, graceful degradation, and structured logging. Use when designing error strategies, building resilient APIs, or reviewing error management.
Error Handling Patterns
Ship resilient software. Handle errors at boundaries, fail fast and loud, never swallow exceptions silently.
Error Handling Philosophy
Principle
Description
Fail Fast
Detect errors early — validate inputs at the boundary, not deep in business logic
Fail Loud
Errors must be visible — log them, surface them, alert on them
Handle at Boundaries
Catch and translate errors at layer boundaries (controller, middleware, gateway)
Let It Crash
For unrecoverable state, crash and restart (Erlang/OTP philosophy)
Be Specific
Catch specific error types, never bare catch or except
Provide Context
Every error carries enough context to diagnose without reproducing
Installation
OpenClaw / Moltbot / Clawbot
npx clawhub@latest install error-handling
Error Types
Operational errors — network timeouts, invalid user input, file not found, DB connection lost. Handle gracefully.
Programmer errors — TypeError, null dereference, assertion failures. Fix the code — don't catch and suppress.
// Operational — handle gracefullytry{constdata=awaitfetch('/api/users');}catch(err){if(err.code==='ECONNREFUSED')returnfallbackData;throwerr;// re-throw unexpected errors}// Programmer — let it crash, fix the bugconstuser=null;user.name;// TypeError — don't try/catch this
Language Patterns
Language
Mechanism
Anti-Pattern
JavaScript
try/catch, Promise.catch, Error subclasses
.catch(() => {}) swallowing errors
Python
Exceptions, context managers (with)
Bare except: catching everything
Go
error returns, errors.Is/As, fmt.Errorf wrapping
_ = riskyFunction() ignoring error
Rust
Result<T, E>, Option<T>, ? operator
.unwrap() in production code
JavaScript — Error Subclasses
classAppErrorextendsError{constructor(message,code,statusCode,details={}){super(message);this.name=this.constructor.name;this.code=code;this.statusCode=statusCode;this.details=details;this.isOperational=true;}}classNotFoundErrorextendsAppError{constructor(resource,id){super(`${resource} not found`,'NOT_FOUND',404,{ resource, id });}}classValidationErrorextendsAppError{constructor(errors){super('Validation failed','VALIDATION_ERROR',422,{ errors });}}
Go — Error Wrapping
funcGetUser(idstring) (*User, error) {
row:=db.QueryRow("SELECT * FROM users WHERE id = $1", id)
varuserUseriferr:=row.Scan(&user.ID, &user.Name); err!=nil {
iferrors.Is(err, sql.ErrNoRows) {
returnnil, fmt.Errorf("user %s: %w", id, ErrNotFound)
}
returnnil, fmt.Errorf("querying user %s: %w", id, err)
}
return&user, nil
}