3

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

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

3
  • Have you read through Express' own guide on error handling? Also, related: Express js error handling, Catch all uncaughtException for Node js app and Node.js Best Practice Exception Handling Or, is there a specific scenario that you're trying to handle errors for that doesn't seem to be covered by those? Commented May 6, 2018 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. Commented May 6, 2018 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. Commented May 6, 2018 at 17:43

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The tool to restart them on crashes is called a “process manager.” I’m using PM2 for that.
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

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.