Skip to content
This repository has been archived by the owner on Aug 19, 2021. It is now read-only.

Commit

Permalink
feat(displayName): fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
spudly committed Mar 22, 2017
1 parent d61d948 commit 0981360
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 38 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import ErrorSubclass from 'error-subclass';

class MyError extends ErrorSubclass {

static displayName = 'MyError'; // optional. survives minification

// Add extra properties/methods if desired:

get code() {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"devDependencies": {
"@spudly/eslint-config": "^5.0.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.11.6",
"babelify": "^7.3.0",
"browserify": "^14.0.0",
Expand All @@ -15,7 +16,8 @@
},
"babel": {
"presets": [
"es2015"
"es2015",
"stage-2"
]
},
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/ErrorSubclass.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const ErrorSubclass = function ErrorSubclass(...args) {
const _error = Error.apply(this, args);

Object.defineProperty(this, 'message', {value: message});
Object.defineProperty(this, 'name', {value: this.constructor.name});
Object.defineProperty(this, 'name', {
value: this.constructor.displayName || this.constructor.name,
});

if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
Expand Down
12 changes: 7 additions & 5 deletions test/ErrorSubclassTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import ErrorSubclass from '../src/ErrorSubclass';

const ERROR_MESSAGE = '__MESSAGE__';

class SubSubError extends ErrorSubclass {}
class SubSubError extends ErrorSubclass {
static displayName = 'SubSubErrorDisplayName';
}

const {captureStackTrace} = Error;

Expand All @@ -29,9 +31,9 @@ const tests = {
ok(instance instanceof SubSubError);
},

'should have a name': () => {
'should have a name that matches the displayName': () => {
const instance = new SubSubError(ERROR_MESSAGE);
equal(instance.name, 'SubSubError');
equal(instance.name, 'SubSubErrorDisplayName');
},

'should have a message': () => {
Expand All @@ -42,15 +44,15 @@ const tests = {
'has a toString() method': () => {
const instance = new SubSubError(ERROR_MESSAGE);
equal(typeof instance.toString, 'function');
equal(instance.toString(), `SubSubError: ${ERROR_MESSAGE}`);
equal(instance.toString(), `SubSubErrorDisplayName: ${ERROR_MESSAGE}`);
},

'when captureStackTrace is supported, should have a stack': () => {
equal(typeof Error.captureStackTrace, 'function');
const instance = new SubSubError(ERROR_MESSAGE);
ok(instance.stack);
equal(typeof instance.stack, 'string');
ok(instance.stack.match(`SubSubError: ${ERROR_MESSAGE}`));
ok(instance.stack.match(`SubSubErrorDisplayName: ${ERROR_MESSAGE}`));
},

// 'when captureStackTrace is not supported, should have a stack': () => {
Expand Down
Loading

0 comments on commit 0981360

Please sign in to comment.