Propagating async errors to Express error handlers
async error forwarding.
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.
WHAT THIS TESTS Whether you understand the difference between synchronous and asynchronous error capture in Express and can route async failures to centralized handling.
A GOOD ANSWER COVERS Express 4 automatically catches errors thrown synchronously inside a handler, but a rejected promise or a throw inside an async function escapes that mechanism, so the request hangs or crashes unless you forward the error explicitly. The direct way is try/catch around the await and calling next(err) in the catch. To avoid repeating that everywhere, wrap each async handler in a higher-order asyncHandler that calls the function and attaches .catch(next), so any rejection is forwarded to the error middleware. Many teams use express-async-errors or the asyncHandler pattern. Express 5 improves this by awaiting returned promises and forwarding rejections to next automatically, so a plain throw in an async handler reaches the error handler.
COMMON WRONG ANSWERS Assuming Express 4 catches async throws automatically, calling next() without passing the error, or sending a response inside catch instead of delegating to the centralized handler.
LIKELY FOLLOW-UPS The difference between Express 4 and 5 behavior, how asyncHandler works internally, why next(err) is required, and avoiding double responses.
ONE CONCRETE EXAMPLE const asyncHandler = fn => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next); then app.get('/users/:id', asyncHandler(async (req, res) => { const user = await User.findById(req.params.id); if (!user) throw new NotFoundError(); res.json(user); })). The wrapper turns any rejection or throw into next(err), so the centralized error middleware formats the response.
Read the original → expressjs.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.