ut1l
Version:
Utility stuff
72 lines (64 loc) • 1.61 kB
JavaScript
var O, createCreateThrowable, createTopThrowable, throwableConstr, throwableProto;
O = require("./object");
throwableProto = {
name: "Error",
toString: function() {
if (this.message != null) {
return "" + this.name + ": " + this.message;
} else {
return this.name;
}
}
};
createTopThrowable = O(throwableProto);
throwableConstr = function(message) {
var e;
this.message = message;
e = Error.call(this, message);
this.stack = e.stack;
};
createCreateThrowable = function(name, parent) {
var proto;
if (parent == null) {
parent = createTopThrowable;
}
proto = parent();
if (name != null) {
proto.name = name;
}
return O(throwableConstr, proto);
};
createCreateThrowable.c4tch = function() {
var action, arg, args, idx, onError, throwables, _i, _len;
args = arguments;
throwables = [];
for (idx = _i = 0, _len = args.length; _i < _len; idx = ++_i) {
arg = args[idx];
if (arg.prototype instanceof createTopThrowable) {
throwables.push(arg);
} else {
break;
}
}
if (throwables.length === 0) {
throwables.push(createTopThrowable);
}
action = args[idx];
onError = args[idx + 1];
return function() {
var e, t, _j, _len1;
try {
return action.apply(this, arguments);
} catch (_error) {
e = _error;
for (_j = 0, _len1 = throwables.length; _j < _len1; _j++) {
t = throwables[_j];
if (e instanceof t) {
return (onError != null ? onError.call(this, e) : void 0);
}
}
throw e;
}
};
};
module.exports = createCreateThrowable;