Lesson 16 / 25
Async Error Handling
Forward errors from async handlers with next, wrappers or Express 5's automatic promise handling.
The gotcha
In Express 4, a rejected promise inside an async route handler does not reach your error middleware; it must be forwarded with next(err). Express 5 (now the default on npm) forwards it automatically.
Catch and forward
Wrap the body in try/catch and call next(err) — or use a small helper to avoid repeating it.
const asyncHandler = (fn) => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
app.get('/users/:id', asyncHandler(async (req, res) => {
const user = await db.users.findById(req.params.id);
if (!user) {
const err = new Error('User not found');
err.status = 404;
throw err;
}
res.json(user);
}));Express 5 fixes this
Express 5 automatically forwards rejected promises from async handlers to next(err) — the wrapper becomes unnecessary once you're on Express 5.