Skip to content

Commit b14d34d

Browse files
pvdlggr2m
authored andcommitted
feat: add details to error messages
1 parent eda41f0 commit b14d34d

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

lib/definitions/errors.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const url = require('url');
2+
const pkg = require('../../package.json');
3+
4+
const homepage = url.format({...url.parse(pkg.homepage), ...{hash: null}});
5+
const linkify = file => `${homepage}/blob/master/${file}`;
6+
7+
module.exports = {
8+
EINVALIDNPMPUBLISH: ({npmPublish}) => ({
9+
message: 'Invalid `npmPublish` option.',
10+
details: `The [npmPublish option](${linkify('README.md#npmpublish')}) option, if defined, must be a \`Boolean\`.
11+
12+
Your configuration for the \`npmPublish\` option is \`${npmPublish}\`.`,
13+
}),
14+
EINVALIDTARBALLDIR: ({tarballDir}) => ({
15+
message: 'Invalid `tarballDir` option.',
16+
details: `The [tarballDir option](${linkify('README.md#tarballdir')}) option, if defined, must be a \`String\`.
17+
18+
Your configuration for the \`tarballDir\` option is \`${tarballDir}\`.`,
19+
}),
20+
EINVALIDPKGROOT: ({pkgRoot}) => ({
21+
message: 'Invalid `pkgRoot` option.',
22+
details: `The [pkgRoot option](${linkify('README.md#pkgroot')}) option, if defined, must be a \`String\`.
23+
24+
Your configuration for the \`pkgRoot\` option is \`${pkgRoot}\`.`,
25+
}),
26+
EINVALIDNPMTOKEN: ({registry}) => ({
27+
message: 'Invalid npm token.',
28+
details: `The [npm token](${linkify(
29+
'README.md#npm-registry-authentication'
30+
)}) configured in the \`NPM_TOKEN\` environment variable must be a valid [token](https://docs.npmjs.com/getting-started/working_with_tokens) allowing to publish to the registry \`${registry}\`.
31+
32+
If you are using Two-Factor Authentication, make configure the \`auth-only\` [level](https://docs.npmjs.com/getting-started/using-two-factor-authentication#levels-of-authentication) is supported. **semantic-release** cannot publish with the default \`auth-and-writes\` level.
33+
34+
Please make sure to set the \`NPM_TOKEN\` environment variable in your CI with the exact value of the npm token.`,
35+
}),
36+
37+
ENOPKGNAME: () => ({
38+
message: 'Missing `name` property in `package.json`.',
39+
details: `The \`package.json\`'s [name](https://docs.npmjs.com/files/package.json#name) property is required in order to publish a package to the npm registry.
40+
41+
Please make sure to add a valid \`name\` for your package in your \`package.json\`.`,
42+
}),
43+
ENOPKG: () => ({
44+
message: 'Missing `package.json` file.',
45+
details: `A [package.json file](https://docs.npmjs.com/files/package.json) at the root of your project is required to release on npm.
46+
47+
Please follow the [npm guideline](https://docs.npmjs.com/getting-started/creating-node-modules) to create a valid \`pacakge.json\` file.`,
48+
}),
49+
};

lib/get-error.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const SemanticReleaseError = require('@semantic-release/error');
2+
const ERROR_DEFINITIONS = require('./definitions/errors');
3+
4+
module.exports = (code, ctx = {}) => {
5+
const {message, details} = ERROR_DEFINITIONS[code](ctx);
6+
return new SemanticReleaseError(message, code, details);
7+
};

lib/get-pkg.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const readPkg = require('read-pkg');
22
const AggregateError = require('aggregate-error');
3-
const SemanticReleaseError = require('@semantic-release/error');
3+
const getError = require('./get-error');
44

55
module.exports = async pkgRoot => {
66
const errors = [];
@@ -10,17 +10,19 @@ module.exports = async pkgRoot => {
1010
pkg = await readPkg(pkgRoot);
1111

1212
if (!pkg.name) {
13-
errors.push(new SemanticReleaseError('No "name" found in package.json.', 'ENOPKGNAME'));
13+
errors.push(getError('ENOPKGNAME'));
1414
}
1515
} catch (err) {
1616
if (err.code === 'ENOENT') {
17-
errors.push(new SemanticReleaseError('A package.json file is required to release on npm.', 'ENOPKG'));
17+
errors.push(getError('ENOPKG'));
1818
} else {
1919
errors.push(err);
2020
}
2121
}
22+
2223
if (errors.length > 0) {
2324
throw new AggregateError(errors);
2425
}
26+
2527
return pkg;
2628
};

lib/verify-auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const execa = require('execa');
22
const normalizeUrl = require('normalize-url');
33
const AggregateError = require('aggregate-error');
4-
const SemanticReleaseError = require('@semantic-release/error');
4+
const getError = require('./get-error');
55
const getRegistry = require('./get-registry');
66
const setNpmrcAuth = require('./set-npmrc-auth');
77

@@ -20,7 +20,7 @@ module.exports = async (
2020
try {
2121
await execa('npm', ['whoami', '--registry', registry]);
2222
} catch (err) {
23-
throw new AggregateError([new SemanticReleaseError('Invalid npm token.', 'EINVALIDNPMTOKEN')]);
23+
throw new AggregateError([getError('EINVALIDNPMTOKEN', {registry})]);
2424
}
2525
}
2626
};

lib/verify-config.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
const {isString, isUndefined, isBoolean} = require('lodash');
2-
const SemanticReleaseError = require('@semantic-release/error');
2+
const getError = require('./get-error');
33

44
module.exports = ({npmPublish, tarballDir, pkgRoot}) => {
55
const errors = [];
66
if (!isUndefined(npmPublish) && !isBoolean(npmPublish)) {
7-
errors.push(
8-
new SemanticReleaseError('The "npmPublish" options, if defined, must be a Boolean.', 'EINVALIDNPMPUBLISH')
9-
);
7+
errors.push(getError('EINVALIDNPMPUBLISH', {npmPublish}));
108
}
119

1210
if (!isUndefined(tarballDir) && !isString(tarballDir)) {
13-
errors.push(
14-
new SemanticReleaseError('The "tarballDir" options, if defined, must be a String.', 'EINVALIDTARBALLDIR')
15-
);
11+
errors.push(getError('EINVALIDTARBALLDIR', {tarballDir}));
1612
}
1713

1814
if (!isUndefined(pkgRoot) && !isString(pkgRoot)) {
19-
errors.push(new SemanticReleaseError('The "pkgRoot" options, if defined, must be a String.', 'EINVALIDPKGROOT'));
15+
errors.push(getError('EINVALIDPKGROOT', {pkgRoot}));
2016
}
17+
2118
return errors;
2219
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"Gregor Martynus (https://twitter.com/gr2m)"
1717
],
1818
"dependencies": {
19-
"@semantic-release/error": "^2.1.0",
19+
"@semantic-release/error": "^2.2.0",
2020
"aggregate-error": "^1.0.0",
2121
"execa": "^0.9.0",
2222
"fs-extra": "^5.0.0",

0 commit comments

Comments
 (0)