I am using a third party library. Which is using node domain
for error handling.
If the callback function passed to that third party library have any error, It end-up in calling my callback multiple times.
Example code is:
var startFunction = function (callback) {
//Call thirdParty function and wait for response
thirdPartyFunction(function (error, data) {
console.log("Called with");
console.log(arguments);
//Assume there is an error in my callback function
setTimeout(function () {
dd
callback.apply(null);
}, 2000);
});
}
//ThirdParty function don't modify anything here
var thirdPartyFunction = function (callback) {
var Domain = require("domain");
var d = require('domain').create();
d.on('error', function (er) {
console.log("hi");
callback.apply(null, er);
});
d.run(function () {
setTimeout(function () {
callback.apply(null, [null, "Hello"]);
}, 1000);
});
};
startFunction(function () {
console.log("Got response")
});
We reported this bug to third party lib and they have modified the source code. Like:
d.on('error', function (er) {
if (isCalled == false) {
isCalled = true;
} else {
return;
}
console.log("hi");
callback.apply(null, er);
});
Now problem of function getting called multiple times is solved. But final callback is never getting called.
How to handle this behavior of node ?
If third party lib modify there code to, It result in application crash. Putting a wrapper domain also not help.
d.on('error', function (er) {
if (isCalled == false) {
isCalled = true;
} else {
throw new Error("Getting called");
return;
}
console.log("hi");
callback.apply(null, er);
});
What is the best method of handing such cases in node ?