-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using "domains" to catch async errors in all the node helper functions #324
Comments
This is an interesting idea. @kriskowal, what do you think of these semantics? Would they be desirable? I'm not sure myself... leaning toward "no, too much magic." Note that the following already works with Q: "use strict";
var Q = require("q");
var domain = require("domain");
var d = domain.create();
d.on("error", function (err) {
console.log("error caught", err);
});
d.run(function () {
Q.nfcall(function() {
setTimeout(function() {
throw new Error("not catched");
}, 100);
})
.then(function() {
console.log("ok");
})
.fail(function() {
console.log("fail");
});
}); |
It is very interesting. However, I don’t think it’s practicable in Q. For one, we provide It breaks my heart, because this is very clever and useful, but it would be wise to decline. |
FWIW I've really started to come around to this idea as a way of plugging a "hole" in the promises + Node.js love story. I was talking with @piscisaureus at NodeConf who wants to make some changes to domains to make them more robust and nice to work with, and part of his idea was vaguely promise-like, but a key part of that was this domain safety net. It is something we may explore in a separate package or something in the future, creating super-promises augmented by domains that can wrap Node functions to make them ergonomic, Stay tuned. |
FWIW, this won't ever work with domains, because the reason you must restart with domains is the same reason you must restart without them: the core call stack is not allowed to unwind, creating an undefined state. It's also coincidentally the same reason why promises catching all exceptions can actually be a liability, since exceptions from core, or exceptions with a slice of core frames sandwiched between userland frames in its call stack should not be caught. There are two ways to solve this, wrap the userland boundary in try/catch blocks to guarantee core stacks always unwind and don't catch / rethrow TRESUR errors from core (which is what trycatch does), or a Generators + Asynchrony Implementation that separates errors from exceptions, in addition to catching JSON.parse exceptions and never ever throwing. Either way, we're now using Q at LinkedIn, and eventually we'll be using Generators + Promises, so I'd like to raise awareness of this common misunderstanding of domains since without a solution like trycatch's it would actually be a bug. @domenic If you remember, I explained all of this to you at nodeconf after your domains presentation included the same inaccurate information. |
and the output is:
However it would be nice if the error could be catched. This can be accomplished by using the "domain" module.
Now the output is
tyrion@aws:~/src/uni$ node test.js fail
The text was updated successfully, but these errors were encountered: