what are generic way to handle error in nodejs

questionswhat are generic way to handle error in nodejs
panav asked 2 years ago

I want to implement generic error handling in nodejs api. what is standard practices for it.

2 Answers
pradeep answered 2 years ago

You can implement middleware for it see below code
// development error handler
// will print stacktrace
if (app.get(‘env’) === ‘development’) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render(‘error’, {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render(‘error’, {
message: err.message,
error: {}
});
});
 

pradeep answered 4 months ago

You can ho ahead with blog http://meanstack.tech/2017/06/27/what-are-generic-way-to-handle-error-in-node-js/
simple and best approach to handle error in node js