Open brijeshhroy opened 1 year ago
this webpage states: "For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next()
function, where Express will catch and process them"
So, I guess when we call next()
, we are passing the error to express
I am not 100% sure
@brijeshhroy the next(error)
will pass the error to the express error handling middleware and as we have a custom errorHandler as a middleware, the error will be passed there. This is how it is intended. Now what happens here is that when the code runs next(error)
, the control will indeed go to notFound
but notFound
only takes 2 arguments (req, res)
and based on the express error handling docs, an exception handler needs to accept err
in the function argument. Thus the control will skip notFound
and go to the errorHandlerMiddleware
.
I hope i was able to explain it clearly and help you solve your doubts.
In the project of task-manager , this is the source-code in middleware/async.js
And this is the code in app.js
Now the line ABC deom async.js shall execute
app.use(notFound)
, but instead it executesapp.use(errorHandlerMiddleWare)
. Why is it so ?