Skip to content

added contextName option #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ node_modules

# Garbage files
.DS_Store

.idea
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ declare namespace PluginError {
* By default it uses the `stack` of the original error if you used one, otherwise it captures a new stack.
*/
stack?: string;

/**
* Error context
* Default: 'plugin'
*/
contextName?: string;
}

/**
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var extend = require('extend-shallow');
var differ = require('arr-diff');
var union = require('arr-union');

var nonEnum = ['message', 'name', 'stack'];
var nonEnum = ['message', 'name', 'stack', 'contextName'];
var ignored = union(nonEnum, ['__safety', '_stack', 'plugin', 'showProperties', 'showStack']);
var props = ['fileName', 'lineNumber', 'message', 'name', 'plugin', 'showProperties', 'showStack', 'stack'];
var props = ['fileName', 'lineNumber', 'message', 'name', 'contextName', 'plugin', 'showProperties', 'showStack', 'stack'];

function PluginError(plugin, message, options) {
if (!(this instanceof PluginError)) {
Expand Down Expand Up @@ -38,6 +38,9 @@ function PluginError(plugin, message, options) {
if (!this.name) {
this.name = 'Error';
}
if (!this.contextName) {
this.contextName = 'plugin';
}
if (!this.stack) {

/**
Expand Down Expand Up @@ -139,7 +142,9 @@ PluginError.prototype.toString = function() {
// Format the output message
function message(msg, thisArg) {
var sig = colors.red(thisArg.name);
sig += ' in plugin ';
sig += ' in ';
sig += thisArg.contextName;
sig += ' ';
sig += '"' + colors.cyan(thisArg.plugin) + '"';
sig += '\n';
sig += msg;
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ describe('PluginError()', function() {
done();
});

it('should default the context name to plugin', function(done) {
var realErr = { message: 'something broke' };
var err = new PluginError('test', realErr);
expect(err.toString()).toContain(' in plugin ');
done();
});

it('should say in task when we set the context name to task', function(done) {
var realErr = { message: 'something broke', contextName: 'task' };
var err = new PluginError('test', realErr);
expect(err.toString()).toContain(' in task ');
done();
});

it('should print the plugin name in toString', function(done) {
var err = new PluginError('test', 'something broke');
expect(err.toString()).toContain('test');
Expand Down