Reputation: 1507
I have a route
app.get("/some/url/", function(req, res){ res.sendStatus(404) })
I am trying to handle or catch this 404 in express error handling middleware and it is not working
app.use(function(err, req, res, next){do something here})
Any help how do i capture the error thrown by route in the middleware.
Upvotes: 2
Views: 4555
Reputation: 774
app.get("/some/url/", function(req, res) {
// Generate Error yourself.
throw new Error("Generated Error.");
});
// global error handler
app.use(function(err, req, res, next) {
console.dir(err);
if(err) {
// Your Error Status code and message here.
res.status(500).send('Something broke!');
}
// Send Some valid Response
});
Upvotes: 4