How do you pass errors to the error handling middleware from route handlers or other middleware?

Responsive Ad Header

Question

Grade: Education Subject: Support
How do you pass errors to the error handling middleware from route handlers or other middleware?
Asked by:
96 Viewed 96 Answers

Answer (96)

Best Answer
(340)
To pass an error to the error handling middleware, you call the `next()` function with the error object as its argument: `next(err)`. This immediately skips any remaining route handlers or middleware in the current stack and jumps to the first matching error handling middleware. This is the standard way to propagate errors in Express.js.