index.js
1.24 KB
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
/* jshint expr: true */
var util = require('util');
module.exports = function(name, parameters, options) {
options = options || {};
options.captureStackTrace = options.captureStackTrace === undefined ? true : false;
options.inherits = options.inherits || Error;
var ctor = function() {
if (!(this instanceof ctor)) {
var constructorArgs = Array.prototype.slice.call(arguments);
constructorArgs.unshift(ctor);
return new (ctor.bind.apply(ctor, constructorArgs))();
}
options.inherits.call(this);
if (options.captureStackTrace) {
Error.captureStackTrace && Error.captureStackTrace(this, arguments.callee);
}
copy(parameters, this);
var msg = arguments[0];
if (msg) {
var args = Array.prototype.slice.call(arguments);
if (args.length > 1 && typeof args[args.length - 1] == 'object') {
var instanceParams = args.pop();
copy(instanceParams, this);
}
this.message = util.format.apply(util, args);
}
this.name = name;
};
util.inherits(ctor, options.inherits);
return ctor;
};
function copy(from, to) {
if (from) {
for (var key in from) {
if (from.hasOwnProperty(key)) {
to[key] = from[key];
}
}
}
return to;
}