Hacker News new | ask | show | jobs
by fibertbh 5114 days ago
The domain module looks fantastic: http://nodejs.org/api/domain.html

"Domains provide a way to handle multiple different IO operations as a single group. If any of the event emitters or callbacks registered to a domain emit an error event, or throw an error, then the domain object will be notified, rather than losing the context of the error in the process.on('uncaughtException') handler, or causing the program to exit with an error code."

This will be great for things like request handlers, initialization code and other "loose" blocks of code where you don't really care about success but you do care about errors not propogating.

3 comments

I've been reading back and forth between this post and the docs and I'm still not really getting it.

Is this just a way of registering a single callback to respond to error events that could be emitted by different operations? What are some examples when this would be better than handling error events individually?

Is it sort of like a try/catch for error events?

> Is it sort of like a try/catch for error events?

Yes, but it's also sort of like a try/catch for thrown errors.

    d = domain.create()
    d.on('error', function(e) {
      console.error('an error: ', e)
    });
    d.run(function() {
      throw new Error("whoops")
    })
would console.log, rather than crashing the program.

When an EventEmitter is bound to a domain, their error events and any errors thrown while emitting an event on them will be handled by the domain object that they're bound to.

Thanks! It is marked experimental, and we really do mean that. The API won't change in 0.8, but it probably will change somewhat in the next stable release. Please try it out and let us know what you think, especially in what ways it is lacking, how it actually helps or hurts in real apps, etc.
That description at the start of the docs is unhelpful.

After scanning the rest of the docs I get the impression that domains are weak containers for resource cleanup (not just IO), paired with an error handling context.