app.use(function(req, res) {
res.status(404);
data.wrapAndRender('error.dust', {code: 404});
});
app.use(function(error, req, res) {
console.error(error.stack);
res.status(500);
data.wrapAndRender('error.dust', {code: 500});
});
What I usually do is have all my middleware and routes call next(err) when something goes wrong, with err being a custom error object that includes enough info for my error handling middleware to figure out the right status code and message, then the error handling middleware handles logging and responds with that info to the client (sanitized of course).
--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.
It's not that verbose. It actually streamlines your control flow a lot, letting each function do one thing well.
For example, given a defined NotFoundError, I can just say
if(!user)
return next(new NotFoundError());
in the callback from the database or ORM query, immediately exiting that handler and going to my error handling middleware. The NotFoundError class would already define the default status and message, and the middleware can use that to configure a generic response.
--