I am wonder if there is a best practice for propagating internal server errors back to the user. I.E. not the actual error (I log that server side), but at least a 500 and a message.
I currently have:
app.use(function(err, req, res, next) {
console.error(err.stack);
res.send(500, 'Internal server error, please contact server administrator.');
});
Which is great except that if an error is thrown my server crashes (expected).
So I am currently propagating any and all errors back to the express route, ie.
app.get('/api/users', function (req, res, next) {
User.getUsers(function (err, users) { // err comes back in function
// dont throw err here in callback
// throw err;
// Instead call next with the error to let express now something bad happened
if (err) return next(err);
res.json(users);
});
});
This works great except that I have 30 routes that have the same line of code.
if (err) return next(err);
Not a huge deal, but make me wonder if there is a 'better' way.
Thanks.