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

Commit

Permalink
Expose option to override properties included on Plugin Error, copy a…
Browse files Browse the repository at this point in the history
…ll properties from original error by default
  • Loading branch information
mtscout6 committed Jul 10, 2014
1 parent a44e450 commit 2703722
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
19 changes: 15 additions & 4 deletions lib/PluginError.js
@@ -1,5 +1,6 @@
var util = require('util');
var colors = require('./colors');
var uniq = require('lodash.uniq');

// 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
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -12,6 +12,7 @@
"lodash._reinterpolate": "^2.4.1",
"lodash.template": "^2.4.1",
"minimist": "^0.2.0",
"lodash.uniq": "~2.4.1",
"multipipe": "^0.1.0",
"through2": "^0.5.0",
"vinyl": "^0.2.1"
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 2703722

Please sign in to comment.