vishwaefor / article-hub

This is a REST API using NodeJS Express + MongoDB
Apache License 2.0
0 stars 37 forks source link

Update error handler to give an JSON always #9

Open vishwaefor opened 5 years ago

vishwaefor commented 5 years ago

Steps

update app.js

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // JSON error
  res.status(err.status || 500);
  res.json({ errors: [{ msg: err.message }] }); // change render to json method
});