Skip to content

Commit 25082a4

Browse files
authored
fix(*): throw Error objects to help with debugging (#1045)
1 parent 673476f commit 25082a4

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

lib/auth.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,21 @@ import { clearCachedConfig, encryptValue, getMergedConfig, getNcurcPath } from '
77

88
export default lazy(auth);
99

10-
function errorExit(message) {
11-
process.stderr.write(`${message}\n`);
12-
process.exit(1);
13-
}
14-
1510
function check(username, token, format = /^[A-Za-z0-9_]+$/) {
1611
if (typeof username !== 'string') {
17-
errorExit(`username must be a string, received ${typeof username}`);
12+
throw new Error(`username must be a string, received ${typeof username}`);
1813
}
1914
if (!/^[a-zA-Z0-9-]+$/.test(username)) {
20-
errorExit(
15+
throw new Error(
2116
'username may only contain alphanumeric characters or hyphens, ' +
2217
`received ${username}`
2318
);
2419
}
2520
if (typeof token !== 'string') {
26-
errorExit(`token must be a string, received ${typeof token}`);
21+
throw new Error(`token must be a string, received ${typeof token}`);
2722
}
2823
if (!format.test(token)) {
29-
errorExit(`token is misformatted: ${token}`);
24+
throw new Error(`token is misformatted: ${token}`);
3025
}
3126
}
3227

@@ -51,7 +46,7 @@ async function tryCreateGitHubToken(githubAuth) {
5146
noDeviceFlow: true
5247
});
5348
} catch (e) {
54-
errorExit(`Could not get token: ${e.message}`);
49+
throw new Error(`Could not get token: ${e.message}`, { cause: e });
5550
}
5651
return credentials;
5752
}
@@ -93,7 +88,7 @@ async function auth(
9388
get jenkins() {
9489
const { username, jenkins_token } = getMergedConfig();
9590
if (!username || !jenkins_token) {
96-
errorExit(
91+
throw new Error(
9792
'Get your Jenkins API token in https://ci.nodejs.org/me/security ' +
9893
'and run the following command to add it to your ncu config: ' +
9994
'ncu-config --global set -x jenkins_token'

test/unit/auth.test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,40 +62,39 @@ describe('auth', async function() {
6262
await runAuthScript(
6363
{},
6464
[FIRST_TIME_MSG],
65-
'Could not get token: Bad credentials\n', 'run-auth-error'
65+
/Could not get token: Bad credentials\n/, 'run-auth-error'
6666
);
6767
});
6868

6969
it('does not accept a non-string username', async function() {
7070
await runAuthScript(
7171
{ HOME: { username: {}, token: '0123456789abcdef' } },
7272
[],
73-
'username must be a string, received object\n'
73+
/username must be a string, received object\n/
7474
);
7575
});
7676

7777
it('does not accept a non-string token', async function() {
7878
await runAuthScript(
7979
{ HOME: { username: 'nyancat', token: 42 } },
8080
[],
81-
'token must be a string, received number\n'
81+
/token must be a string, received number\n/
8282
);
8383
});
8484

8585
it('does not accept an invalid username format', async function() {
8686
await runAuthScript(
8787
{ HOME: { username: ' ^^^ ', token: '0123456789abcdef' } },
8888
[],
89-
'username may only contain alphanumeric characters or hyphens, ' +
90-
'received ^^^ \n'
89+
/username may only contain alphanumeric characters or hyphens, received {2}\^\^\^ \n/
9190
);
9291
});
9392

9493
it('does not accept an invalid token format', async function() {
9594
await runAuthScript(
9695
{ HOME: { username: 'nyancat', token: '@fhqwhgads' } },
9796
[],
98-
'token is misformatted: @fhqwhgads\n'
97+
/token is misformatted: @fhqwhgads\n/
9998
);
10099
});
101100

0 commit comments

Comments
 (0)