0

Looks like in node.js, process.domain is null at the top level. What this means to me is that there is no "top-level domain" that errors can be thrown into. This leads me to my question:

var d = require('domain').create();
d.on('error', function(er) {
    console.log(er)
})
d.run(function() {
    setTimeout(function(){
        throw new Error("?") // how do you throw this such that it doesn't hit the on 'error' handler but instead is treated like the error below would be?
    }, 0)
})

//throw new Error("the kind of error handling i want")

Basically, I'm trying to store process.domain at one point, and then throw the exception in the context of that domain when the domain-context has changed. The problem is, since there is no "top-level domain", its unclear to me how to emulate how an exception would be handled at the top level normally.

Ideas?

B T
  • 57,525
  • 34
  • 189
  • 207
  • Have you looked at [this question](http://stackoverflow.com/questions/7310521/node-js-best-practice-exception-handling)? – Pointy May 24 '14 at 21:14
  • Yes indeed, in fact I posted an answer to that question. – B T May 24 '14 at 21:15

1 Answers1

0

Here's one way, but I don't like it - it seems pretty cludgy since it relies on what I consider to be a bad design of domains:

var d = require('domain').create();
d.on('error', function(er) {
    console.log("never gets here, oddly enough")
})
d.run(function() {
    var d1 = require('domain').create()
    d1.on('error', function(e) {
        throw e
    })
    d1.run(function() {
        setTimeout(function() {
            throw new Error("error I want to throw at the top-level")
        })
    })
})
B T
  • 57,525
  • 34
  • 189
  • 207