Skip to content

Commit dc6ea59

Browse files
pvdlggr2m
authored andcommitted
feat: Throw regular Error for unexpected errors
BREAKING CHANGE: Do not wrap unexpected errors in a `@semantic-release/error`. This way, in case of unexpected error (wrong release rule for example) `semantic-release` will fail and return with an exit code
1 parent bd19733 commit dc6ea59

File tree

9 files changed

+28
-153
lines changed

9 files changed

+28
-153
lines changed

lib/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const loadParserConfig = require('./load/parser-config');
22
const loadReleaseRules = require('./load/release-rules');
3-
const parse = require('./parse');
43
const analyzeCommit = require('./analyze-commit');
54
const compareReleaseTypes = require('./compare-release-types');
65
const RELEASE_TYPES = require('./default/release-types');
@@ -31,8 +30,8 @@ module.exports = async (pluginConfig = {}, {commits}, callback) => {
3130
let releaseType = null;
3231

3332
commits.every(rawCommit => {
34-
const commit = parse(rawCommit.message, config);
3533
let commitReleaseType;
34+
const commit = parser(rawCommit.message, config);
3635

3736
// Determine release type based on custom releaseRules
3837
if (releaseRules) {

lib/load/parser-config.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const {promisify} = require('util');
22
const importFrom = require('import-from');
33
const {mergeWith} = require('lodash');
4-
const SemanticReleaseError = require('@semantic-release/error');
54
const conventionalChangelogAngular = require('conventional-changelog-angular');
65

76
/**
@@ -17,17 +16,9 @@ module.exports = async ({preset, config, parserOpts}) => {
1716

1817
if (preset) {
1918
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
20-
try {
21-
loadedConfig = importFrom.silent(__dirname, presetPackage) || importFrom(process.cwd(), presetPackage);
22-
} catch (err) {
23-
throw new SemanticReleaseError(`Preset: "${preset}" does not exist: ${err.message}`, err.code);
24-
}
19+
loadedConfig = importFrom.silent(__dirname, presetPackage) || importFrom(process.cwd(), presetPackage);
2520
} else if (config) {
26-
try {
27-
loadedConfig = importFrom.silent(__dirname, config) || importFrom(process.cwd(), config);
28-
} catch (err) {
29-
throw new SemanticReleaseError(`Config: "${config}" does not exist: ${err.message}`, err.code);
30-
}
21+
loadedConfig = importFrom.silent(__dirname, config) || importFrom(process.cwd(), config);
3122
} else if (!parserOpts) {
3223
loadedConfig = conventionalChangelogAngular;
3324
}

lib/load/release-rules.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const importFrom = require('import-from');
2-
const SemanticReleaseError = require('@semantic-release/error');
32
const RELEASE_TYPES = require('../default/release-types');
43

54
/**
@@ -21,19 +20,15 @@ module.exports = ({releaseRules}) => {
2120
: releaseRules;
2221

2322
if (!Array.isArray(loadedReleaseRules)) {
24-
throw new SemanticReleaseError(
25-
'Error in commit-analyzer configuration: "releaseRules" must be an array of rules',
26-
'EINVALIDCONFIG'
27-
);
23+
throw new Error('Error in commit-analyzer configuration: "releaseRules" must be an array of rules');
2824
}
2925

3026
loadedReleaseRules.forEach(rule => {
3127
if (RELEASE_TYPES.indexOf(rule.release) === -1) {
32-
throw new SemanticReleaseError(
28+
throw new Error(
3329
`Error in commit-analyzer configuration: "${rule.release}" is not a valid release type. Valid values are: ${JSON.stringify(
3430
RELEASE_TYPES
35-
)}`,
36-
'EINVALIDRELEASE'
31+
)}`
3732
);
3833
}
3934
});

lib/parse.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
}
1313
},
1414
"dependencies": {
15-
"@semantic-release/error": "^2.0.0",
1615
"conventional-changelog-angular": "^1.4.0",
1716
"conventional-commits-parser": "^2.0.0",
1817
"import-from": "^2.1.0",

test/integration.test.js

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {promisify} from 'util';
22
import test from 'ava';
3-
import SemanticReleaseError from '@semantic-release/error';
43
import commitAnalyzer from '../lib/index';
54

65
test('Parse with "conventional-changelog-angular" by default', async t => {
@@ -134,65 +133,43 @@ test('Ignore malformatted commits and process valid ones', async t => {
134133
t.is(releaseType, 'patch');
135134
});
136135

137-
test('Throw "SemanticReleaseError" if "preset" doesn`t exist', async t => {
138-
const error = await t.throws(
139-
promisify(commitAnalyzer)({preset: 'unknown-preset'}, {}),
140-
/Preset: "unknown-preset" does not exist:/
141-
);
136+
test('Throw error if "preset" doesn`t exist', async t => {
137+
const error = await t.throws(promisify(commitAnalyzer)({preset: 'unknown-preset'}, {}));
142138

143-
t.true(error instanceof SemanticReleaseError);
144139
t.is(error.code, 'MODULE_NOT_FOUND');
145140
});
146141

147-
test('Throw "SemanticReleaseError" if "releaseRules" is not an Array or a String', async t => {
148-
const error = await t.throws(
142+
test('Throw error if "releaseRules" is not an Array or a String', async t => {
143+
await t.throws(
149144
promisify(commitAnalyzer)({releaseRules: {}}, {}),
150145
/Error in commit-analyzer configuration: "releaseRules" must be an array of rules/
151146
);
152-
153-
t.true(error instanceof SemanticReleaseError);
154-
t.is(error.code, 'EINVALIDCONFIG');
155147
});
156148

157-
test('Throw "SemanticReleaseError" if "releaseRules" option reference a requierable module that is not an Array or a String', async t => {
158-
const error = await t.throws(
149+
test('Throw error if "releaseRules" option reference a requierable module that is not an Array or a String', async t => {
150+
await t.throws(
159151
promisify(commitAnalyzer)({releaseRules: './test/fixtures/release-rules-invalid'}, {}),
160152
/Error in commit-analyzer configuration: "releaseRules" must be an array of rules/
161153
);
162-
163-
t.true(error instanceof SemanticReleaseError);
164-
t.is(error.code, 'EINVALIDCONFIG');
165154
});
166155

167-
test('Throw "SemanticReleaseError" if "config" doesn`t exist', async t => {
156+
test('Throw error if "config" doesn`t exist', async t => {
168157
const commits = [{message: 'Fix: First fix (fixes #123)'}, {message: 'Update: Second feature (fixes #456)'}];
169-
const error = await t.throws(
170-
promisify(commitAnalyzer)({config: 'unknown-config'}, {commits}),
171-
/Config: "unknown-config" does not exist:/
172-
);
158+
const error = await t.throws(promisify(commitAnalyzer)({config: 'unknown-config'}, {commits}));
173159

174-
t.true(error instanceof SemanticReleaseError);
175160
t.is(error.code, 'MODULE_NOT_FOUND');
176161
});
177162

178-
test('Throw "SemanticReleaseError" if "releaseRules" reference invalid commit type', async t => {
179-
const error = await t.throws(
163+
test('Throw error if "releaseRules" reference invalid commit type', async t => {
164+
await t.throws(
180165
promisify(commitAnalyzer)({preset: 'eslint', releaseRules: [{tag: 'Update', release: 'invalid'}]}, {}),
181166
/Error in commit-analyzer configuration: "invalid" is not a valid release type\. Valid values are:\[?.*\]/
182167
);
183-
184-
t.is(error.code, 'EINVALIDRELEASE');
185-
t.true(error instanceof SemanticReleaseError);
186168
});
187169

188-
test('Handle error in "conventional-changelog-parser" and wrap in "SemanticReleaseError"', async t => {
170+
test('Re-Throw error from "conventional-changelog-parser"', async t => {
189171
const commits = [{message: 'Fix: First fix (fixes #123)'}, {message: 'Update: Second feature (fixes #456)'}];
190-
const error = await t.throws(
191-
promisify(commitAnalyzer)({parserOpts: {headerPattern: '\\'}}, {commits}),
192-
/Error in conventional-changelog-parser: Invalid regular expression:/
193-
);
194-
195-
t.true(error instanceof SemanticReleaseError);
172+
await t.throws(promisify(commitAnalyzer)({parserOpts: {headerPattern: '\\'}}, {commits, logger: t.context.logger}));
196173
});
197174

198175
test('Accept an undefined "pluginConfig"', async t => {

test/load-parser-config.test.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import test from 'ava';
2-
import SemanticReleaseError from '@semantic-release/error';
32
import loadParserConfig from './../lib/load/parser-config';
43

54
/**
@@ -70,22 +69,14 @@ test(loadConfig, 'express');
7069
test(loadPreset, 'jshint');
7170
test(loadConfig, 'jshint');
7271

73-
test('Throw "SemanticReleaseError" if "config" doesn`t exist', async t => {
74-
const error = await t.throws(
75-
loadParserConfig({config: 'unknown-config'}),
76-
/Config: "unknown-config" does not exist:/
77-
);
72+
test('Throw error if "config" doesn`t exist', async t => {
73+
const error = await t.throws(loadParserConfig({config: 'unknown-config'}));
7874

79-
t.true(error instanceof SemanticReleaseError);
8075
t.is(error.code, 'MODULE_NOT_FOUND');
8176
});
8277

83-
test('Throw "SemanticReleaseError" if "preset" doesn`t exist', async t => {
84-
const error = await t.throws(
85-
loadParserConfig({preset: 'unknown-preset'}),
86-
/Preset: "unknown-preset" does not exist:/
87-
);
78+
test('Throw error if "preset" doesn`t exist', async t => {
79+
const error = await t.throws(loadParserConfig({preset: 'unknown-preset'}));
8880

89-
t.true(error instanceof SemanticReleaseError);
9081
t.is(error.code, 'MODULE_NOT_FOUND');
9182
});

test/load-release-rules.test.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import test from 'ava';
2-
import SemanticReleaseError from '@semantic-release/error';
32
import loadReleaseRules from './../lib/load/release-rules';
43
import testReleaseRules from './fixtures/release-rules';
54

@@ -21,32 +20,25 @@ test('Return undefined if "releaseRules" not set', t => {
2120
t.is(releaseRules, undefined);
2221
});
2322

24-
test('Throw "SemanticReleaseError" if "releaseRules" reference invalid commit type', t => {
25-
const error = t.throws(
23+
test('Throw error if "releaseRules" reference invalid commit type', t => {
24+
t.throws(
2625
() => loadReleaseRules({releaseRules: [{tag: 'Update', release: 'invalid'}]}),
2726
/Error in commit-analyzer configuration: "invalid" is not a valid release type\. Valid values are:\[?.*\]/
2827
);
2928

30-
t.is(error.code, 'EINVALIDRELEASE');
31-
t.true(error instanceof SemanticReleaseError);
3229
});
3330

34-
test('Throw "SemanticReleaseError" if "releaseRules" is not an Array or a String', t => {
35-
const error = t.throws(
31+
test('Throw error if "releaseRules" is not an Array or a String', t => {
32+
t.throws(
3633
() => loadReleaseRules({releaseRules: {}}, {}),
3734
/Error in commit-analyzer configuration: "releaseRules" must be an array of rules/
3835
);
39-
40-
t.true(error instanceof SemanticReleaseError);
41-
t.is(error.code, 'EINVALIDCONFIG');
4236
});
4337

44-
test('Throw "SemanticReleaseError" if "releaseRules" option reference a requierable module that is not an Array or a String', t => {
45-
const error = t.throws(
38+
test('Throw error if "releaseRules" option reference a requierable module that is not an Array or a String', t => {
39+
t.throws(
4640
() => loadReleaseRules({releaseRules: './test/fixtures/release-rules-invalid'}),
4741
/Error in commit-analyzer configuration: "releaseRules" must be an array of rules/
4842
);
4943

50-
t.true(error instanceof SemanticReleaseError);
51-
t.is(error.code, 'EINVALIDCONFIG');
5244
});

test/parse.test.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)