This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
PluginError.js
75 lines (58 loc) · 2.01 KB
/
PluginError.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var util = require('util');
var colors = require('./colors');
var _ = require('lodash');
// wow what a clusterfuck
var parseOptions = function(plugin, message, opt) {
if (!opt) opt = {};
if (typeof plugin === 'object') {
opt = plugin;
} else if (message instanceof Error) {
opt.error = message;
opt.plugin = plugin;
if (!opt.properties) {
opt.properties = Object.keys(message);
}
} else if (typeof message === 'object') {
opt = message;
opt.plugin = plugin;
} else if (typeof opt === 'object') {
opt.plugin = plugin;
opt.message = message;
}
var defaultProperties = ['name', 'message', 'fileName', 'lineNumber', 'stack'];
if (!opt.properties) {
opt.properties = [];
}
opt.properties = _.uniq(defaultProperties.concat(opt.properties));
return opt;
};
function PluginError(plugin, message, opt) {
if (!(this instanceof PluginError)) throw new Error('Call PluginError using new');
Error.call(this);
var options = parseOptions(plugin, message, opt);
this.plugin = options.plugin;
this.showStack = options.showStack === true;
// if options has an error, grab details from it
if (options.error) {
options.properties.forEach(function(prop) {
if (prop in options.error) this[prop] = options.error[prop];
}, this);
}
// options object can override
options.properties.forEach(function(prop) {
if (prop in options) this[prop] = options[prop];
}, this);
// defaults
if (!this.name) this.name = 'Error';
// TODO: figure out why this explodes mocha
if (!this.stack) Error.captureStackTrace(this, arguments.callee || this.constructor);
if (!this.plugin) throw new Error('Missing plugin name');
if (!this.message) throw new Error('Missing error message');
}
util.inherits(PluginError, Error);
PluginError.prototype.toString = function () {
var sig = this.name+' in plugin \''+colors.cyan(this.plugin)+'\'';
var msg = this.showStack ? (this._stack || this.stack) : this.message;
return sig+'\n'+msg;
};
module.exports = PluginError;