Skip to content

Commit

Permalink
fix: Handling errors before retrying request (jdalrymple#142) [skip-ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Aug 15, 2018
1 parent 34f7a7b commit bc3b366
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/infrastructure/RequestHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,20 @@ async function getPaginated(service, endpoint, options = {}, sleepOnRateLimit =

return data;
} catch (err) {
if(
!err.response ||
!err.response.headers ||
!err.response.headers['retry-after'] ||
parseInt(err.statusCode, 10) != 429
) throw error;

const sleepTime = parseInt(err.response.headers['retry-after'], 10);
if (sleepOnRateLimit && parseInt(err.statusCode, 10) === 429
&& sleepTime) {
await wait(sleepTime * 1000);
return getPaginated(service, endpoint, options, sleepOnRateLimit);
}
throw err;

if (!sleepTime) throw error;

await wait(sleepTime * 1000);

return getPaginated(service, endpoint, options);
}
}

Expand Down

0 comments on commit bc3b366

Please sign in to comment.