Skip to content
tezvyn:
Menu

Handling async errors in Express middleware

Source: interviewMediumHow cards are made

Summary

routing async errors into Express.

Key points

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.

What's really being asked

This checks whether you know that Express middleware error handling is built around the next callback and that async rejections need explicit forwarding in the classic versions.

The full answer

Middleware receives req, res, and next. For asynchronous work like a database fetch, you mark the function async and await the operation inside a try block. On success you attach the result and call next to continue. On failure, the catch block calls next with the error, which routes control to Express's error-handling middleware, the special four-argument handler with err, req, res, and next. The critical point in Express 4 is that throwing or rejecting inside an async function is not caught by Express automatically, so an unhandled rejection leaves the request hanging and never sends a response. To avoid repeating try/catch everywhere, teams use a small higher-order wrapper that takes an async handler, runs it, and pipes any rejection into next. Express 5 improves this by forwarding rejected Promises to the error handler automatically.

The mistakes people make

Assuming Express 4 automatically catches async throws, calling next without the error so it falls through to normal middleware, sending two responses, or swallowing the error silently in catch without forwarding it.

What usually comes next

What distinguishes error-handling middleware by its four parameters, how the async wrapper works, behavior changes in Express 5, and why you must not call next after already sending a response.

A concrete example

A middleware loads the current user: it awaits userService.findById inside try, sets req.user, and calls next on success. If the database query rejects, the catch calls next with the error, and a central error handler responds with an appropriate status and JSON body. Without that try/catch in Express 4, the rejection would be unhandled, the central handler would never run, and the client would wait indefinitely until a timeout.

Interview question

In classic Express 4, how should an error from an awaited database call inside async middleware reach the error handler?

  • a.Throw it and let Express catch it automatically
  • b.Ignore it, since unhandled rejections are routed by default
  • c.Catch it and pass it to next(err) so the error-handling middleware runsCorrect
  • d.Call res.send with the error directly from the middleware
Why?

Express 4 does not auto-catch async throws, so you must catch and forward with next(err) to trigger the four-argument error handler. Throwing alone leaves the request hanging.

Just read this? Test yourself on what you have been reading.

Read the original → expressjs.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles