Error Handling
56 bites tagged Error Handling — interview questions with model answers, and 60-second explainers.
How do you handle errors across piped streams safely?
Pipe connects streams but errors don't auto-propagate, must listen on each stream. Use pipeline() helper for auto-cleanup. stream lifecycle and error propagation understanding.
Promise .catch() versus async/await try...catch
.catch() handles rejection for all preceding chain steps and reads functionally; try/catch reads synchronously and can scope errors per await, but only catches awaited rejections. Async error-handling models.
Robust network error handling strategy
Classify errors as 4xx, 5xx, or connectivity; retry transient failures with backoff, surface actionable messages, and check NetInfo. differentiated error handling. one generic catch that retries everything, including 400s.
Designing an API for power vs novice personas
Power users want control, batching, and precise errors; novices want defaults, guardrails, and guidance; both want consistency. Whether personas drive concrete API tradeoffs.
Pinpointing validation errors in nested request data
Use schema validation that reports a path, collect all errors not just the first, return a 400 with field paths and messages. structured error reporting for nested input.
Handling uncaughtException and unhandledRejection
Listen on process for uncaughtException and unhandledRejection, log the error, stop accepting new work, drain in-flight requests, then exit non-zero for a supervisor to restart. process-level last-resort error handling.
Operational versus programmer errors in Node.js
Operational errors are expected runtime conditions you handle and respond to; programmer errors are bugs that may corrupt state, so you log and gracefully restart. error classification and recovery policy.
Custom Error classes and centralized handling
Custom Error subclasses carry a statusCode and flag, the central handler inspects instanceof or statusCode to set the HTTP code and JSON shape, defaulting unknown errors to 500. structured error design.
Propagating async errors to Express error handlers
Express does not auto-catch rejected promises, so catch and call next(err), or wrap handlers in an asyncHandler that forwards rejections; Express 5 awaits handlers automatically. async error forwarding.
Centralized error handling in an Express API
A final four-arg error middleware, an asyncHandler wrapper to funnel promise rejections via next, a custom error class with statusCode, returning uniform JSON. designing one error path.
404 vs 500: missing resource vs server failure
A missing resource returns 404 Not Found (client asked for something absent); a database failure returns 500 Internal Server Error (server-side fault). 4xx versus 5xx semantics.
Express error-handling middleware signature
(err, req, res, next) with err first, identified by arity, placed last after all routes. recognizing the four-argument signature. omitting the err parameter or registering it before the routes it should catch.
Centralized error-handling middleware
Error middleware takes four args err, req, res, next, is defined last, and runs when next(err) is called or sync errors throw. Express error flow. omitting the err parameter so Express treats it as normal middleware.
Promise.all vs Promise.allSettled
All rejects on the first failure; allSettled always fulfills with a status/value or reason per input. Use allSettled when partial success is acceptable. choosing fail-fast vs collect-all.
Comparing the three async error-handling styles
Callbacks pass err as first arg; Promises route errors to catch; async/await uses try/catch; an unhandled rejection can crash the Node process. fluency across async error styles.
Handling async errors in Express middleware
Await inside try/catch and call next(err) on failure, or wrap the handler in an async error adapter; never let a rejected Promise go uncaught. routing async errors into Express.
Handling a 401 error in an LLM agent's tool call
Catch the tool error, return a structured observation to the LLM, and distinguish recoverable retries from terminal failures needing re-plan or escalation. Robust agent error handling.
do-try-catch versus Optional return types
Throwing functions with do/try/catch convey why something failed via typed Error values; Optionals only say success or absence. Use throws when the caller needs the reason. error modeling choices.
anyhow versus thiserror in Rust error handling
Anyhow gives one opaque dynamic error type for applications where you mostly propagate and report; thiserror derives concrete typed enums for libraries so callers can match on variants. idiomatic error-design judgment.
Go if err != nil versus Rust's ? operator
Go's explicit checks are verbose but make every error site visible; Rust's ? propagates concisely while still forcing the error into the type, reducing boilerplate. judgment on error-handling ergonomics.
Go (T, error) versus Rust Result for error handling
Go returns a separate error value you may ignore; Rust wraps success or error in one Result enum the compiler forces you to handle. Go favors simplicity, Rust favors compile-enforced safety. understanding of explicit error models.
Handling Result and errors in a Rust web handler
Handler returns Result, the ? operator early-returns errors, a custom error type implements IntoResponse to map to 500, success returns 200 with data. Result-based error handling in web handlers.
Structuring a Go CLI that fetches a URL
Parse args with the flag package, http.Get the URL, check err and status, defer resp.Body.Close, copy body to stdout, exit non-zero on failure. basic Go CLI, HTTP, and error handling.
Building a safe Rust wrapper over an unsafe C API
Hide extern calls behind a safe module, own the resource in a struct with Drop calling the C free, return Result mapping C error codes, use NewType/NonNull and PhantomData. FFI encapsulation patterns.
Get Error Handling bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.