fix: retry 429 with retry-after

This commit is contained in:
Rhys Arkins 2018-05-28 16:10:23 +02:00
parent 11d5a8b02e
commit 1210300dd2
2 changed files with 11 additions and 4 deletions

View file

@ -215,10 +215,14 @@ async function getDependency(name, retries = 5) {
return getDependency(name, retries - 1); return getDependency(name, retries - 1);
} }
if (err.statusCode === 429) { if (err.statusCode === 429) {
// This is bad if it ever happens, so we should error if (retries <= 0) {
logger.error({ err }, 'npm registry failure: too many requests'); logger.error({ err }, 'npm registry failure: too many requests');
throw new Error('registry-failure'); throw new Error('registry-failure');
} }
const retryAfter = err.headers['retry-after'] || 30;
await delay(1000 * (retryAfter + 1));
return getDependency(name, retries - 1);
}
if (err.statusCode === 408) { if (err.statusCode === 408) {
if (retries <= 0) { if (retries <= 0) {
logger.warn({ err }, 'npm registry failure: timeout, retries=0'); logger.warn({ err }, 'npm registry failure: timeout, retries=0');

View file

@ -88,12 +88,15 @@ describe('api/npm', () => {
expect(e).toBeDefined(); expect(e).toBeDefined();
}); });
it('should throw error for 429', async () => { it('should throw error for 429', async () => {
nock('https://registry.npmjs.org')
.get('/foobar')
.reply(429);
nock('https://registry.npmjs.org') nock('https://registry.npmjs.org')
.get('/foobar') .get('/foobar')
.reply(429); .reply(429);
let e; let e;
try { try {
await npm.getDependency('foobar'); await npm.getDependency('foobar', 1);
} catch (err) { } catch (err) {
e = err; e = err;
} }