There are roughly four kinds of errors you run into in node.
synchronous code, and throw is usually limited to application logic, synchronous decisions being made from information already on hand. They can also arise from programmer error – accessing properties or functions of
undefined are among the most common errors I see.
If you are calling a callback in an asychronous context provided by another module or user, it’s smart to guard these with
try/catch blocks, and direct the error into your own error emission path.
The naive implementation can fail badly:
The above will crash if
intermediateCallback throws an exception. Instead, guard this:
This is important since a synchronous throw in an asynchronously called function ends up becoming the next kind of error:
asynchronous calls and throw will crash your process. If you’re using domains, then it will fall back to the domain error handler, but in both cases, this is either uncatchable – a
try/catch block will have already exited the block before the call is made – or you are completely without context when you catch it, so you won’t be able to usefully clean up resources allocated during the request that eventually failed. The only hope is to catch it in a
process.on('uncaughtException handler or domain handler, clean up what you can – close or delete temp files or undo whatever is being worked on – and crash a little more cleanly.
Anything meant to be called asynchronously should never throw. Instead, callbacks should be called with an error argument:
callback(new Error("Error message here")); This makes the next kind of error,
asynchronous calls with an error parameter in the callback receive the error as a parameter – either as a separate callback for errors, or in node, much more commonly the “error first” style:
This forces the programmer to handle or propagate the error at each stage.
The reason the error argument is first is so that it’s hard to ignore. If your first parameter is
err and you don’t use it, you are likely to crash if you get an error, since you’ll only look at the success path.
iferr module, you can get promise-like short-circuiting of errors:
Using promises also gives this short-circuit error behavior, but you get the error out of the promise with the
.catch method. In some implementations, if an error happens and you haven’t set up what happens to it, it will throw after a process tick. Similarly, event emitters with unhandled error events throw an exception. This leads to the fourth kind of error:
asynchronous event emitters or promises, and error handlers
An event emitter that can emit an
error event should have a handler set up.
If you don’t do this, your process will crash or the domain handler will fire, and you should crash there. (Unless your promises don’t handle this case, in which case your error is lost and you never know it happened. Also not good.)