Question presented to candidate: "An async Express route handler throws an error with no try/catch around it. What happens to that request, and does your answer change between Express 4 and Express 5?"
What a strong answer should cover:
- Node.js error handling is not one mechanism — it differs by context: synchronous code uses
try/catch; callback-style async code uses the error-first(err, result)convention; Promise-based/async code usestry/catcharoundawait, or.catch(). - 📌 A version-specific, verifiable gap: in Express 4, a synchronous throw inside a route handler is automatically caught and routed to centralized error-handling middleware — but an async handler that throws (or rejects) with no manual
next(err)call is not caught automatically; it crashes the process as an uncaught exception. Express 5 fixes this specific gap, automatically forwarding a rejected promise from an async handler tonext(). - Centralized error-handling middleware in Express is a 4-argument function
(err, req, res, next)— Express recognizes this specific arity and routes errors to it, distinct from ordinary 3-argument middleware. - The operational vs. programmer error distinction (covered fully in its own dedicated question) determines the right response once an error is caught: an operational error should be handled and answered with an appropriate status code; a programmer error/bug should generally be logged and allowed to crash the process for a supervisor to restart, rather than papered over.
uncaughtException/unhandledRejectionhandlers at the process level are a last-resort safety net — logging and exiting — not a substitute for handling errors correctly at the point they actually occur.- A precise answer names the practical mitigation for the Express-4 async gap specifically: wrap every async handler in a small utility (a manual
try/catchcallingnext(e), or a wrapper likeexpress-async-handler) until/unless the codebase is on Express 5.
Clarifying questions expected:
- "Which Express major version, or a different framework entirely?" — the async-handler auto-catch behavior is genuinely version-dependent, verified above.
- "Is the concern about a specific caught error's response, or the broader process-level safety net?" — these are different layers of the same overall topic.
Code / implementation expected: Yes — actually demonstrating the Express 4 async-handler gap (a real crash) versus the manual-next(e) fix is the concrete, convincing part of the answer.