Skip to content

Commit

Permalink
feat: add the details property
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Feb 10, 2018
1 parent 2961ac8 commit 30631f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ throw new SemanticReleaseError('An error happened');
// With error message and error code
throw new SemanticReleaseError('An error happened', 'ECODE');

// With error message, error code and details
throw new SemanticReleaseError('An error happened', 'ECODE', 'Here is some suggestions to solve this error.');

// With inheritance
class InheritedError extends SemanticReleaseError {
constructor(message, code, newProperty) {
constructor(message, code, newProperty, details) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.code = code;
this.details = details;
this.newProperty = 'newProperty';
}
}

throw new InheritedError('An error happened', 'ECODE');
throw new InheritedError('An error happened', 'ECODE', 'Here is some suggestions to solve this error.');
```
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module.exports = class SemanticReleaseError extends Error {
constructor(message, code) {
constructor(message, code, details) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = 'SemanticReleaseError';
this.code = code;
this.details = details;
this.semanticRelease = true;
}
};
13 changes: 13 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ test('Sets message and code', t => {
t.true(error instanceof SemanticReleaseError);
});

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

t.is(error.code, code);
t.is(error.message, message);
t.is(error.details, details);
t.true(error.semanticRelease);
t.true(error instanceof SemanticReleaseError);
});

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

0 comments on commit 30631f0

Please sign in to comment.