Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Merge 164b968 into 4c94141
Browse files Browse the repository at this point in the history
  • Loading branch information
mtscout6 committed Jul 2, 2014
2 parents 4c94141 + 164b968 commit 4045210
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
19 changes: 15 additions & 4 deletions lib/PluginError.js
@@ -1,5 +1,6 @@
var util = require('util');
var colors = require('./colors');
var _ = require('lodash');

// wow what a clusterfuck
var parseOptions = function(plugin, message, opt) {
Expand All @@ -9,13 +10,25 @@ var parseOptions = function(plugin, message, opt) {
} 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;
};

Expand All @@ -29,17 +42,15 @@ function PluginError(plugin, message, opt) {
this.plugin = options.plugin;
this.showStack = options.showStack === true;

var properties = ['name', 'message', 'fileName', 'lineNumber', 'stack'];

// if options has an error, grab details from it
if (options.error) {
properties.forEach(function(prop) {
options.properties.forEach(function(prop) {
if (prop in options.error) this[prop] = options.error[prop];
}, this);
}

// options object can override
properties.forEach(function(prop) {
options.properties.forEach(function(prop) {
if (prop in options) this[prop] = options[prop];
}, this);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -14,7 +14,8 @@
"through2": "^0.5.0",
"dateformat": "^1.0.7-1.2.3",
"multipipe": "^0.1.0",
"minimist": "^0.2.0"
"minimist": "^0.2.0",
"lodash": "~2.4.1"
},
"devDependencies": {
"mocha": "^1.17.0",
Expand Down
22 changes: 21 additions & 1 deletion test/PluginError.js
Expand Up @@ -69,4 +69,24 @@ describe('PluginError()', function(){
done();
});

});
it('should take properties from error by default', function() {
var realErr = new Error('something broke');
realErr.abstractProperty = 'abstract';
var err = new util.PluginError('test', realErr);
err.plugin.should.equal('test');
err.message.should.equal('something broke');
err.abstractProperty.should.equal('abstract');
});

it('should take properties from error unless overridden in options', function() {
var realErr = new Error('something broke');
realErr.abstractProperty = 'abstract';
realErr.notIncludedProperty = 'abstract';
var err = new util.PluginError('test', realErr, {properties: ['abstractProperty']});
err.plugin.should.equal('test');
err.message.should.equal('something broke');
err.abstractProperty.should.equal('abstract');
(err.notIncludedProperty === undefined).should.be.true;
});

});

0 comments on commit 4045210

Please sign in to comment.