1

I'm developing small MEAN application. and I have developed good error handling at angularJS part (UI Part). But still I am lacking to handle error properly from Backend part - ExpressJS (Rather NodeJS). ExpressJS also consume Web Services (AWS).

I have already read different stackoverflow posts regarding 'uncaughtException', 'domains'. Some post suggest to start-node again using packages like forever or node cluster. But I didn't find consolidated solution to handle NODE errors.

Can anyone suggest me end to end proper workflow with some best pattern and practices to be followed to handle NodeJS error. I am looking for error handling and logging mechanism same time.

Thank you very much in advance.

crazyvraj
  • 37
  • 5
  • https://stackoverflow.com/questions/27273915/expressjs-handling-errors-with-middleware?rq=1 https://stackoverflow.com/questions/26470535/error-handling-in-node-js-express-using-promises?rq=1 – Shanoor Jan 04 '16 at 07:17
  • @ShanShan, Thank you. I am referring it. I have also gone through [link](https://www.joyent.com/developers/node/design/errors). https://www.joyent.com/developers/node/design/errors If someone has implemented some pattern and give a ref. that will help a lot. – crazyvraj Jan 04 '16 at 09:23
  • There is an implemenation here: http://stackoverflow.com/a/26471134/5388620 It seems to be a good way to handle ExpressJS errors. – Shanoor Jan 04 '16 at 09:49

1 Answers1

0

You can use global error handler in app.js after you have defined all the routes.

https://expressjs.com/en/guide/error-handling.html

app.use(function (err, req, res, next) {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

In each route throw the error by saying next(err) explicitly if any condition applied or from within callback.

whenever error occurs outside of callback or the ones not defined, express throws them implicitly. You need not handle them seperately if you don't want to.