Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js, catching errors so script doesn't break?

Tags:

node.js

I have a node.js script and anytime an error happens node.js stops running with the error that happened.

What is the proper way of checking errors in node.js so it won't break the script and cause node.js to stop?

like image 507
John Avatar asked Oct 13 '11 23:10

John


2 Answers

You can catch otherwise uncaught errors by setting the following:

process.on('uncaughtException', function (exception) {
   // handle or ignore error
  });

Of course, if you run your script in something like forever or node-supervisor, when your script "stops" it will start back up, which might solve your problem.

like image 125
rob Avatar answered Sep 25 '22 22:09

rob


Catching uncaughtException is no longer recommended.

Using domains are preferred. Using either "uncaughtException" or Domain, restarting the process is highly recommended after handling the exception.

To auto-restart the process, NodeJS provides another library called Cluster which assists in managing the worker threads.

like image 20
neowulf33 Avatar answered Sep 22 '22 22:09

neowulf33