fix(backend): allow calling 'json' again on 403 failure (#4880)

This commit is contained in:
Erez Rokah
2021-01-26 09:00:01 -08:00
committed by GitHub
parent 68fbdd7886
commit 1034086ff6

View File

@ -57,15 +57,16 @@ export const requestWithBackoff = async (
throw new Error(text); throw new Error(text);
} else if (response.status === 403) { } else if (response.status === 403) {
// GitHub too many requests // GitHub too many requests
const { message } = await response.json().catch(() => ({ message: '' })); const json = await response.json().catch(() => ({ message: '' }));
if (message.match('API rate limit exceeded')) { if (json.message.match('API rate limit exceeded')) {
const now = new Date(); const now = new Date();
const nextWindowInSeconds = response.headers.has('X-RateLimit-Reset') const nextWindowInSeconds = response.headers.has('X-RateLimit-Reset')
? parseInt(response.headers.get('X-RateLimit-Reset')!) ? parseInt(response.headers.get('X-RateLimit-Reset')!)
: now.getTime() / 1000 + 60; : now.getTime() / 1000 + 60;
throw new RateLimitError(message, nextWindowInSeconds); throw new RateLimitError(json.message, nextWindowInSeconds);
} }
response.json = () => Promise.resolve(json);
} }
return response; return response;
} catch (err) { } catch (err) {