Update 2014: In retrospect, NodeJS domains are unstable and bizarre. They produce a lot of regional affairs and are not very funny. Promises is probably the best option for handling errors - good promise libraries like Bluebird and Q create good stack traces and Bluebird fast - Promises also have a catch security guarantee.
Update 2017: you definitely need to use async functions for any asynchronous operations that you do, this one time and use this to handle exceptions and use the language built into tools like asynchronous iterators to handle exceptions. By now, the days of writing the callback suffix are long gone.
Use domains .
Domains were introduced in version 0.8 and work, so they are quite new. At 0.10 they are pretty stable. They provide a preferred approach to the uncaughtException event. The awesome domains are obsolete: D. This is not specific to an expression or any other specific structure or library.
In general, domains allow code separation. Everything that you start in the domain will allow the domain to catch it. They allow you to get brief stack traces and tell the server what to do when an error occurs. You can even use domains for specific parts of the server, but keep in mind that exceptions are relatively expensive in the JS world.
var d = domain.create(); d.on('error', function(er) {
I also wrote a simple try / catch for asynchronous exceptions here if you find this interesting :)
source share