It also seemed odd to me but makes sense now because throwing an
exception within an error event handler of domain is equivalent to that
of uncaughtException just like
process.on('uncaughtException', function(e) {
console.log(e.message);
throw new Error('bar');
});
throw new Error('foo');
Throwed exception in uncaughtException is not caught in itself.
You can see its reason for a domain case in
https://github.com/joyent/node/blob/master/lib/domain.js#L51-68
One of the workwrounds is to emit an error event explicitly on the
parent domain as
var domain = require('domain');
var topDomain = domain.create();
var subDomain = domain.create();
topDomain.add(subDomain);
topDomain.on("error", function(err) {
console.log("Top saw an error: " + err.message);
});
subDomain.on("error", function(err) {
console.log("Sub saw an error: " + err.message);
if (this.domain) this.domain.emit('error',new Error("bar"));
});
topDomain.run( function() {
subDomain.run(function() {
throw new Error("foo");
});
});
(2012/07/13 6:45), Mike Schilling wrote:
> I wrote a simple test of nested domains which looks like this:
>
> var topDomain = domain.create();
> var subDomain = domain.create();
> topDomain.add(subDomain);
>
> topDomain.on("error", function(err) {
> console.log("Top saw an error: " + err.message);
> });
>
> subDomain.on("error", function(err) {
> console.log("Sub saw an error: " + err.message);
>
> throw new Error("bar")
> });
>
> topDomain.run( function() {
> subDomain.run(function() {
> throw new Error("foo");
> });
> });
>
> What I was expecting to happen is
>
> 1. subDomain catches the first error (foo) and prints a message
> 2. Since subDomain is a member of topDomain, topDomain catches the
> second error (bar), which is thrown from a subDomain event. Now
> topDomain prints out a message.
> 3. Done
> 4. That is, the output should look like:
>
> Sub saw an error: foo
>
> Top saw an error: bar
>
>
> That's what happens when I run node under the IntelliJ debugger (no
> breakpoints set, just run it to completion.) But if I run node without
> the debugger, either under IntelliJ or directly from the command line,
> the output is
>
>
> Sub saw an error: foo
>
> /Users/mike/Documents/workspace-sts-2.9.0.RELEASE/mms/src/js/scripts/testDomains.js:107
>
> throw new Error("bar")
>
> ^
>
> Error: bar
>
> at Domain.<anonymous>
> (/Users/mike/Documents/workspace-sts-2.9.0.RELEASE/mms/src/js/scripts/testDomains.js:107:11)
>
> at Domain.EventEmitter.emit (events.js:88:17)
>
> at process.uncaughtHandler (domain.js:60:20)
>
> at process.EventEmitter.emit (events.js:115:20)
>
>
> That is, the first error is caught but not the second one. This
> is v0.8.1 on Mac.
>
> --
> Job Board:
http://jobs.nodejs.org/
> Posting guidelines:
>
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to
nod...@googlegroups.com
> To unsubscribe from this group, send email to
>
nodejs+un...@googlegroups.com
> For more options, visit this group at
>
http://groups.google.com/group/nodejs?hl=en?hl=en