From fc203bcc89e21488a95d2cf7df62e65ff19635b2 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Wed, 16 May 2018 06:01:02 +0200 Subject: [PATCH] refactor(github): do not retry 401 Bad Credentials --- lib/platform/github/gh-got-wrapper.js | 21 ++++++++++----------- test/platform/github/gh-got-wrapper.spec.js | 19 +++++++++++++++++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/platform/github/gh-got-wrapper.js b/lib/platform/github/gh-got-wrapper.js index e7f6ca9ee5..4d7758fb65 100644 --- a/lib/platform/github/gh-got-wrapper.js +++ b/lib/platform/github/gh-got-wrapper.js @@ -96,18 +96,17 @@ async function get(path, opts, retries = 5) { } else if ( err.statusCode === 401 && err.message && - err.message.indexOf('Bad credentials') !== -1 + err.message.includes('Bad credentials') ) { - if (retries > 0) { - logger.info( - { statusCode: err.statusCode, message: err.message }, - `Retrying request` - ); - - await delay(5000 / retries); - - return get(path, opts, retries - 1); - } + logger.debug( + { + err, + message: err.message, + body: err.response ? err.response.body : undefined, + }, + 'Bad credentials' + ); + throw new Error('bad-credentials'); } throw err; } diff --git a/test/platform/github/gh-got-wrapper.spec.js b/test/platform/github/gh-got-wrapper.spec.js index 2d5dba292d..21081254c6 100644 --- a/test/platform/github/gh-got-wrapper.spec.js +++ b/test/platform/github/gh-got-wrapper.spec.js @@ -74,6 +74,22 @@ describe('platform/gh-got-wrapper', () => { } expect(e).toBeDefined(); }); + it('should throw Bad credentials', async () => { + ghGot.mockImplementationOnce(() => + Promise.reject({ + statusCode: 401, + message: 'Bad credentials. (401)', + }) + ); + let e; + try { + await get('some-url'); + } catch (err) { + e = err; + } + expect(e).toBeDefined(); + expect(e.message).toEqual('bad-credentials'); + }); it('should retry 502s', async () => { ghGot.mockImplementationOnce(() => Promise.reject({ @@ -118,8 +134,7 @@ describe('platform/gh-got-wrapper', () => { ); ghGot.mockImplementationOnce(() => Promise.reject({ - statusCode: 401, - message: 'Bad credentials', + statusCode: 502, }) ); ghGot.mockImplementationOnce(() => -- GitLab