Book Image

Mastering Web Application Development with Express

By : Alexandru Vladutu
Book Image

Mastering Web Application Development with Express

By: Alexandru Vladutu

Overview of this book

Table of Contents (18 chapters)
Mastering Web Application Development with Express
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Handling uncaught exceptions


When errors are not handled, the Node applications crash. To catch unexpected errors, we have two options: to use process.on('uncaughtException', ..) or the native domains API (which might be removed in a future version of Node).

If we decide to catch these unexpected exceptions, it is extremely important to not ignore the error and just move on. Whatever we do with the error, we still need to exit the process because it's in an inconsistent or unrecoverable state.

Here's a small snippet of how we can handle uncaught exceptions:

process.on('uncaughtException', function (err) {
  console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
  console.error(err.stack);
  process.exit(1);
});

throw new Error('bad things happen');