Skip to content

Commit

Permalink
fix: Include stacktrace and name in SemanticReleaseError
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg authored and gr2m committed Oct 29, 2017
1 parent 97137c4 commit 6b978eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
18 changes: 8 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module.exports = SemanticReleaseError;

SemanticReleaseError.prototype = new Error();

function SemanticReleaseError(message, code) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.code = code;
}
module.exports = class SemanticReleaseError extends Error {
constructor(message, code) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.code = code;
}
};
5 changes: 5 additions & 0 deletions test/helpers/throw-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import SemanticReleaseError from '../../index';

export default () => {
throw new SemanticReleaseError('message', 'code');
};
9 changes: 8 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import SemanticReleaseError from '../index';
import throwError from './helpers/throw-error';

test('Instanciates error', t => {
const error = new SemanticReleaseError();
Expand All @@ -14,11 +15,17 @@ test('Sets message', t => {
t.is(error.message, message);
});

test('Sets message and code', function(t) {
test('Sets message and code', t => {
const code = 'ENOFOO';
const message = 'bar';
const error = new SemanticReleaseError(message, code);

t.is(error.code, code);
t.is(error.message, message);
});

test('Include the stacktrace and name', async t => {
const error = await t.throws(() => throwError());
t.regex(error.stack, /helpers\/throw-error/);
t.is(error.name, 'SemanticReleaseError');
});

0 comments on commit 6b978eb

Please # to comment.