From 0856f4bcf682336f8ca5f7ce5042bb7d664a74ff Mon Sep 17 00:00:00 2001 From: Nick Olinger Date: Tue, 28 Jan 2020 02:25:19 +0900 Subject: [PATCH 1/3] handle failed policy set-version --- src/cli/commands/policies.js | 13 ++++++++++--- src/reporters/lang/en.js | 1 + src/util/request-manager.js | 12 ++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/cli/commands/policies.js b/src/cli/commands/policies.js index 62b8617f30..f130577e28 100644 --- a/src/cli/commands/policies.js +++ b/src/cli/commands/policies.js @@ -122,9 +122,16 @@ const {run, setFlags, examples} = buildSubCommands('policies', { bundleUrl = 'https://github.com/yarnpkg/berry/raw/master/packages/berry-cli/bin/berry.js'; bundleVersion = 'berry'; } else { - const releases = await fetchReleases(config, { - includePrereleases: allowRc, - }); + let releases = []; + + try { + releases = await fetchReleases(config, { + includePrereleases: allowRc, + }); + } catch (e) { + reporter.error(e.message); + return; + } const release = releases.find(release => { // $FlowFixMe diff --git a/src/reporters/lang/en.js b/src/reporters/lang/en.js index 5175666d90..e376e969b3 100644 --- a/src/reporters/lang/en.js +++ b/src/reporters/lang/en.js @@ -358,6 +358,7 @@ const messages = { errorExtractingTarball: 'Extracting tar content of $1 failed, the file appears to be corrupt: $0', updateInstalling: 'Installing $0...', hostedGitResolveError: 'Error connecting to repository. Please, check the url.', + unauthorizedResponse: 'Received a 401 from $0. $1', unknownFetcherFor: 'Unknown fetcher for $0', diff --git a/src/util/request-manager.js b/src/util/request-manager.js index 8756139f14..5572d4ee4b 100644 --- a/src/util/request-manager.js +++ b/src/util/request-manager.js @@ -373,6 +373,11 @@ export default class RequestManager { rejectNext(err); }; + const rejectWithoutUrl = function(err) { + err.message = err.message; + rejectNext(err); + } + const queueForRetry = reason => { const attempts = params.retryAttempts || 0; if (attempts >= this.maxRetryAttempts - 1) { @@ -428,6 +433,13 @@ export default class RequestManager { } } + const server = res.caseless.get('server'); + + if (res.statusCode === 401 && server === "GitHub.com") { + const message = `${res.body.message}. If using GITHUB_TOKEN in your env, check that it is valid.` + rejectWithoutUrl(new Error(this.reporter.lang('unauthorizedResponse', server, message))) + } + if (res.statusCode === 401 && res.headers['www-authenticate']) { const authMethods = res.headers['www-authenticate'].split(/,\s*/).map(s => s.toLowerCase()); From 64efa3ce5ac66265ad57c9d21aa1ca1065a6e866 Mon Sep 17 00:00:00 2001 From: Nick Olinger Date: Tue, 28 Jan 2020 02:45:05 +0900 Subject: [PATCH 2/3] linting --- src/util/request-manager.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/request-manager.js b/src/util/request-manager.js index 5572d4ee4b..8b2226ae6a 100644 --- a/src/util/request-manager.js +++ b/src/util/request-manager.js @@ -376,7 +376,7 @@ export default class RequestManager { const rejectWithoutUrl = function(err) { err.message = err.message; rejectNext(err); - } + }; const queueForRetry = reason => { const attempts = params.retryAttempts || 0; @@ -435,9 +435,9 @@ export default class RequestManager { const server = res.caseless.get('server'); - if (res.statusCode === 401 && server === "GitHub.com") { - const message = `${res.body.message}. If using GITHUB_TOKEN in your env, check that it is valid.` - rejectWithoutUrl(new Error(this.reporter.lang('unauthorizedResponse', server, message))) + if (res.statusCode === 401 && server === 'GitHub.com') { + const message = `${res.body.message}. If using GITHUB_TOKEN in your env, check that it is valid.`; + rejectWithoutUrl(new Error(this.reporter.lang('unauthorizedResponse', server, message))); } if (res.statusCode === 401 && res.headers['www-authenticate']) { From 208e40a138fd7701b38ce70d76b05ab477e38b8f Mon Sep 17 00:00:00 2001 From: Nick Olinger Date: Tue, 28 Jan 2020 03:00:10 +0900 Subject: [PATCH 3/3] handle instances where caseless is not defined on response --- src/util/request-manager.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/util/request-manager.js b/src/util/request-manager.js index 8b2226ae6a..fd41bf2100 100644 --- a/src/util/request-manager.js +++ b/src/util/request-manager.js @@ -433,11 +433,9 @@ export default class RequestManager { } } - const server = res.caseless.get('server'); - - if (res.statusCode === 401 && server === 'GitHub.com') { + if (res.statusCode === 401 && res.caseless && res.caseless.get('server') === 'GitHub.com') { const message = `${res.body.message}. If using GITHUB_TOKEN in your env, check that it is valid.`; - rejectWithoutUrl(new Error(this.reporter.lang('unauthorizedResponse', server, message))); + rejectWithoutUrl(new Error(this.reporter.lang('unauthorizedResponse', res.caseless.get('server'), message))); } if (res.statusCode === 401 && res.headers['www-authenticate']) {