Skip to content

Commit 1bbfd5f

Browse files
pvdlggr2m
authored andcommitted
feat: Set a property in SemanticReleaseError constructor to better support inheritance
1 parent 6b978eb commit 1bbfd5f

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
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.
6+
7+
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.
8+
59
[![npm](https://img.shields.io/npm/v/@semantic-release/error.svg)](https://www.npmjs.com/package/@semantic-release/error)
610
[![Greenkeeper badge](https://badges.greenkeeper.io/semantic-release/error.svg)](https://greenkeeper.io/)
711
[![license](https://img.shields.io/github/license/semantic-release/error.svg)](https://github.com/semantic-release/error/blob/master/LICENSE)
@@ -25,4 +29,17 @@ throw new SemanticReleaseError('An error happened');
2529

2630
// With error message and error code
2731
throw new SemanticReleaseError('An error happened', 'ECODE');
32+
33+
// With inheritance
34+
class InheritedError extends SemanticReleaseError {
35+
constructor(message, code, newProperty) {
36+
super(message);
37+
Error.captureStackTrace(this, this.constructor);
38+
this.name = this.constructor.name;
39+
this.code = code;
40+
this.newProperty = 'newProperty';
41+
}
42+
}
43+
44+
throw new InheritedError('An error happened', 'ECODE');
2845
```

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ module.exports = class SemanticReleaseError extends Error {
44
Error.captureStackTrace(this, this.constructor);
55
this.name = this.constructor.name;
66
this.code = code;
7+
this.semanticRelease = true;
78
}
89
};

test/helpers/inherited-error.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import SemanticReleaseError from '../../index';
2+
3+
export default class InheritedError extends SemanticReleaseError {
4+
constructor(message, code, newProperty) {
5+
super(message);
6+
Error.captureStackTrace(this, this.constructor);
7+
this.name = this.constructor.name;
8+
this.code = code;
9+
this.newProperty = 'newProperty';
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import InheritedError from './inherited-error';
2+
3+
export default () => {
4+
throw new InheritedError('message', 'code');
5+
};

test/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import test from 'ava';
22
import SemanticReleaseError from '../index';
33
import throwError from './helpers/throw-error';
4+
import InheritedError from './helpers/inherited-error';
5+
import throwInheritedError from './helpers/throw-inherited-error';
46

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

810
t.true(error instanceof Error);
11+
t.true(error.semanticRelease);
12+
t.true(error instanceof SemanticReleaseError);
913
});
1014

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

1519
t.is(error.message, message);
20+
t.true(error.semanticRelease);
21+
t.true(error instanceof SemanticReleaseError);
1622
});
1723

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

2329
t.is(error.code, code);
2430
t.is(error.message, message);
31+
t.true(error.semanticRelease);
32+
t.true(error instanceof SemanticReleaseError);
2533
});
2634

2735
test('Include the stacktrace and name', async t => {
2836
const error = await t.throws(() => throwError());
2937
t.regex(error.stack, /helpers\/throw-error/);
3038
t.is(error.name, 'SemanticReleaseError');
39+
t.true(error.semanticRelease);
40+
t.true(error instanceof SemanticReleaseError);
41+
});
42+
43+
test('Include "semanticRelease" property in inherited errors', async t => {
44+
const error = await t.throws(() => throwInheritedError());
45+
t.regex(error.stack, /helpers\/throw-inherited-error/);
46+
t.is(error.name, 'InheritedError');
47+
t.true(error instanceof InheritedError);
48+
t.true(error.semanticRelease);
3149
});

0 commit comments

Comments
 (0)