-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatry.js
59 lines (53 loc) · 1.32 KB
/
atry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var domain = require("domain");
var atry = module.exports = function(toRun) {
var dom = domain.create();
var argsWithoutFirst = Array.prototype.slice.call(arguments, 1);
return {
run: function(errHandle) {
dom.run(function() {
try {
toRun.apply(null, argsWithoutFirst);
} catch (err) {
dom.exit();
errHandle(err);
}
});
},
catch: function(errHandle) {
dom.on("error", function(err) {
dom.exit();
return errHandle(err);
})
this.run(errHandle);
return this;
},
ignoreCatch: function() {
dom.on("error", function() {});
this.run(function() {});
}
};
};
module.exports.bind = function(bindFunction) {
return {
catch: function(catchFunction) {
return function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(bindFunction);
atry.apply(atry, args).catch (catchFunction);
}
}
};
}
module.exports.intercept = function(bindFunction) {
return {
catch: function(catchFunction) {
return function(err) {
if(err)
return catchFunction(err);
var args = Array.prototype.slice.call(arguments, 1);
args.unshift(bindFunction);
atry.apply(atry, args).catch (catchFunction);
};
}
};
}