Because Node.js runs on a single process uncaught exceptions are an issue to be aware of when developing applications.
Most of the people let node.js server(s) silently swallow up the errors.
process.on('uncaughtException', function (err) {
console.log(err);
});
This is bad, it will work but:
Root cause will remains unknown, as such will not contribute to resolution of what caused the Exception ( Error ).
In case of database connection ( pool ) gets closed for some reason this will result in constant propagation of errors, meaning that server will be running but it will not reconnect to db.
In case of an " uncaughtException " it is good to restart the server and return it to its initial state, where we know it will work. Exception is logged, application is terminated but since it will be running in a container that will make sure that the server is running we will achieve restarting of the server ( returning to the initial working state ) .
npm install forever -g
forever start app.js
Reason why is it started and why we use forever is after the server is terminated forever process will start the server again.
process.on('uncaughtException', function (err) {
console.log(err);
// some logging mechanisam
// ....
process.exit(1); // terminates process
});
On a side note there was a way also to handle exceptions with Clusters and Domains.
Domains are deprecated more information here.