diff --git a/lib/api/gh-got-retry.js b/lib/api/gh-got-retry.js index ee3bf0fdd53b19636b720d138bf924ed049f4ba9..a1922fcf60644c97274c414d6eca0d10e95b7ae9 100644 --- a/lib/api/gh-got-retry.js +++ b/lib/api/gh-got-retry.js @@ -12,41 +12,27 @@ async function ghGotRetry(path, opts, retries = 5) { const res = await ghGot(path, opts); return res; } catch (err) { - if (retries > 0) { - if ( - err.statusCode === 401 && - err.message && - err.message.indexOf('Bad credentials') === 0 - ) { - logger.warn(`Retrying bad credentials once`); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(180000 / (retries * retries)); - } - return ghGotRetry(path, opts, 0); + if (err.statusCode >= 500 && err.statusCode < 600 && retries > 0) { + logger.debug(`Retrying statusCode ${err.statusCode}`); + // istanbul ignore if + if (process.env.NODE_ENV !== 'test') { + await sleep(5000 / retries); } - if ( - err.statusCode === 403 && - err.message && - err.message.indexOf( - 'You have triggered an abuse detection mechanism' - ) === 0 - ) { - logger.warn(`Retrying abuse detection trigger`); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(180000 / (retries * retries)); - } - return ghGotRetry(path, opts, retries - 1); - } - if (err.statusCode >= 500 && err.statusCode < 600) { - logger.info(`Retrying statusCode ${err.statusCode}`); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(5000 / retries); - } - return ghGotRetry(path, opts, retries - 1); + return ghGotRetry(path, opts, retries - 1); + } + if ( + retries > 0 && + err.statusCode === 403 && + err.message && + err.message.indexOf('You have triggered an abuse detection mechanism') === + 0 + ) { + logger.debug(`Retrying abuse detection trigger`); + // istanbul ignore if + if (process.env.NODE_ENV !== 'test') { + await sleep(180000 / (retries * retries)); } + return ghGotRetry(path, opts, retries - 1); } throw err; } diff --git a/test/api/github.spec.js b/test/api/github.spec.js index 098ab99356f8972e9544a4193c8db9a9d5acae19..58d1d40ae91d64a404e0f599515603120c8ad3b6 100644 --- a/test/api/github.spec.js +++ b/test/api/github.spec.js @@ -95,27 +95,6 @@ describe('api/github', () => { } expect(err.statusCode).toBe(404); }); - it('should retry 401 only once', async () => { - ghGot.mockImplementationOnce(() => - Promise.reject({ - statusCode: 401, - message: 'Bad credentials', - }) - ); - ghGot.mockImplementationOnce(() => - Promise.reject({ - statusCode: 401, - message: 'Bad credentials', - }) - ); - let err; - try { - await github.getInstallations('sometoken'); - } catch (e) { - err = e; - } - expect(err.statusCode).toBe(401); - }); it('should give up after 5 retries', async () => { ghGot.mockImplementationOnce(() => Promise.reject({