3

What is the prefered way of handling uncaughtexception in nodejs application?

FYI, I am having a Express based REST Api nodejs server.

Naveen
  • 351
  • 3
  • 9
  • Have you read through Express' own [guide on error handling](http://expressjs.com/en/guide/error-handling.html)? Also, related: [Express js error handling](https://stackoverflow.com/questions/15684130), [Catch all uncaughtException for Node js app](https://stackoverflow.com/questions/40867345) and [Node.js Best Practice Exception Handling](https://stackoverflow.com/questions/7310521) Or, is there a specific scenario that you're trying to handle errors for that doesn't seem to be covered by those? – Jonathan Lonowski May 06 '18 at 17:28
  • @JonathanLonowski: I have handled the errors using promise-catch and Express error handler. But I am concern about what if there occur some unhandled exception, How it should be hanled? Some says, use process level event, but some dicourage this handling this way. I am confused what is the correct/best way to do thia. – Naveen May 06 '18 at 17:37
  • What's generally discouraged is using the `process` event to allow the process to continue. It's fine to use the event as a chance to retrieve and log additional information before stopping the process, but the process should then still stop (or restart). That information should help you revise other areas of your application's code, so it either doesn't cause the exception or to improve handling of them at the source. – Jonathan Lonowski May 06 '18 at 17:43

2 Answers2

8

Lot of people do this

process.on('uncaughtException', function (err) {
   console.log(err);
});

This is bad. When an uncaught exception is thrown you should consider your application in an unclean state. You can’t reliably continue your program at this point.

Let your application crash, log and restart.

process.on('uncaughtException', function (err) {
  console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
  console.error(err.stack);
  // Send the error log to your email
  sendMail(err);
  process.exit(1);
})

let your application crash in the event of an uncaught exception and use a tool like forever or upstart to restart it (almost) instantly. You can send an email to be aware of such events.

0

an exception should do one of two things:

  1. be caught and handled properly by the program
  2. cause the program to terminate in catastrophic angry failure

if you have uncaught exception that you are trying to get rid of in your program, you either have a bug to fix, or you need to catch that exception and handle it properly

complicated async code, typically involving a mess of disorganized uncaught promises, is often the cause of many uncaught exception problems -- you need to watch how you return your promise chains very carefully

the only permanent solution is to adopt modern async/await code practices to keep your async code maintainable

ChaseMoskal
  • 7,151
  • 5
  • 37
  • 50
  • Yes I agree the code should handle the exception in the first place. but what if, it remained unhandled and bubble uo to the process level? – Naveen May 06 '18 at 17:41