2016-10-10 03:43:44 +02:00
|
|
|
var util = require('util');
|
2016-11-03 01:33:53 +01:00
|
|
|
var colors = require('./colors');
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
// wow what a clusterfuck
|
|
|
|
var parseOptions = function(plugin, message, opt) {
|
2016-11-03 01:33:53 +01:00
|
|
|
if (!opt) opt = {};
|
2016-10-10 03:43:44 +02:00
|
|
|
if (typeof plugin === 'object') {
|
|
|
|
opt = plugin;
|
2016-11-03 01:33:53 +01:00
|
|
|
} else if (message instanceof Error) {
|
|
|
|
opt.error = message;
|
|
|
|
opt.plugin = plugin;
|
|
|
|
} else if (typeof message === 'object') {
|
|
|
|
opt = message;
|
|
|
|
opt.plugin = plugin;
|
|
|
|
} else if (typeof opt === 'object') {
|
2016-10-10 03:43:44 +02:00
|
|
|
opt.plugin = plugin;
|
2016-11-03 01:33:53 +01:00
|
|
|
opt.message = message;
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
return opt;
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
this.plugin = options.plugin;
|
|
|
|
this.showStack = options.showStack === true;
|
|
|
|
|
|
|
|
var properties = ['name', 'message', 'fileName', 'lineNumber', 'stack'];
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
// if options has an error, grab details from it
|
|
|
|
if (options.error) {
|
2016-11-03 01:33:53 +01:00
|
|
|
properties.forEach(function(prop) {
|
|
|
|
if (prop in options.error) this[prop] = options.error[prop];
|
|
|
|
}, this);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// options object can override
|
|
|
|
properties.forEach(function(prop) {
|
|
|
|
if (prop in options) this[prop] = options[prop];
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// defaults
|
|
|
|
if (!this.name) this.name = 'Error';
|
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
// TODO: figure out why this explodes mocha
|
|
|
|
if (!this.stack) Error.captureStackTrace(this, arguments.callee || this.constructor);
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
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 () {
|
2016-11-03 01:33:53 +01:00
|
|
|
var sig = this.name+' in plugin \''+colors.cyan(this.plugin)+'\'';
|
|
|
|
var msg = this.showStack ? (this._stack || this.stack) : this.message;
|
|
|
|
return sig+'\n'+msg;
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = PluginError;
|