The dynamic programmer

Dealing with errors on node.js.

You may be use to handle exceptions/errors in javascript using the try catch construct.

In node.js (since is javascript), you have this construct available and works, most of the time, as expected.

Actually try/catch works exactly as expected all the time, but keep reading.

Things change when you use some modules, specially async modules, that raise an ‘error’ event.
The default action of an ‘error’ event is to print the stack and exit the program. Go ahead check the docs I will wait.

In that case even if you surround the offending code on a try catch block your program will exit while complaining very loudly of whatever happened to brake it.

I bet that, that’s probably not what you will like so, “always” provide a handler for the error event and handle the error accordingly in that handler.

Once you know about it is obvious, it can be tricky sometimes not knowing all the api’s of the different modules.