Skip to content

Commit

Permalink
feat: Set a property in SemanticReleaseError constructor to better su…
Browse files Browse the repository at this point in the history
…pport inheritance
  • Loading branch information
pvdlg authored and gr2m committed Oct 29, 2017
1 parent 6b978eb commit 1bbfd5f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Error type used by all [semantic-release](https://github.com/semantic-release/semantic-release) packages.

Errors of type `SemanticReleaseError` or an inherited type will be considered by [semantic-release](https://github.com/semantic-release/semantic-release) as an expected exception case (no release to be done, running on a PR etc..). That indicate to the `semantic-release` process to stop and exit with the `0` success code.

Any other type of error will be considered by [semantic-release](https://github.com/semantic-release/semantic-release) as an unexpected error (i/o issue, code problem etc...). That indicate to the `semantic-release` process to stop, log the error and exit with the `1` failure code.

[![npm](https://img.shields.io/npm/v/@semantic-release/error.svg)](https://www.npmjs.com/package/@semantic-release/error)
[![Greenkeeper badge](https://badges.greenkeeper.io/semantic-release/error.svg)](https://greenkeeper.io/)
[![license](https://img.shields.io/github/license/semantic-release/error.svg)](https://github.com/semantic-release/error/blob/master/LICENSE)
Expand All @@ -25,4 +29,17 @@ throw new SemanticReleaseError('An error happened');

// With error message and error code
throw new SemanticReleaseError('An error happened', 'ECODE');

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

throw new InheritedError('An error happened', 'ECODE');
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = class SemanticReleaseError extends Error {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.code = code;
this.semanticRelease = true;
}
};
11 changes: 11 additions & 0 deletions test/helpers/inherited-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SemanticReleaseError from '../../index';

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

export default () => {
throw new InheritedError('message', 'code');
};
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import test from 'ava';
import SemanticReleaseError from '../index';
import throwError from './helpers/throw-error';
import InheritedError from './helpers/inherited-error';
import throwInheritedError from './helpers/throw-inherited-error';

test('Instanciates error', t => {
const error = new SemanticReleaseError();

t.true(error instanceof Error);
t.true(error.semanticRelease);
t.true(error instanceof SemanticReleaseError);
});

test('Sets message', t => {
const message = 'foo';
const error = new SemanticReleaseError(message);

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

test('Sets message and code', t => {
Expand All @@ -22,10 +28,22 @@ test('Sets message and code', t => {

t.is(error.code, code);
t.is(error.message, message);
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/);
t.is(error.name, 'SemanticReleaseError');
t.true(error.semanticRelease);
t.true(error instanceof SemanticReleaseError);
});

test('Include "semanticRelease" property in inherited errors', async t => {
const error = await t.throws(() => throwInheritedError());
t.regex(error.stack, /helpers\/throw-inherited-error/);
t.is(error.name, 'InheritedError');
t.true(error instanceof InheritedError);
t.true(error.semanticRelease);
});

0 comments on commit 1bbfd5f

Please sign in to comment.