1
Fork 0

retry requests returning 5xx after 5 seconds

This commit is contained in:
Conduitry 2023-01-24 17:44:34 -05:00
parent 92fb64675a
commit 1fca797207
1 changed files with 11 additions and 5 deletions

View File

@ -6,12 +6,18 @@ const fetch_options_lookup = {};
const fetch_200 = async url => {
const { hostname } = new URL(url);
const fetch_options = await (fetch_options_lookup[hostname] ??= fs.promises.readFile(new URL(`./${hostname}.json`, import.meta.url), 'utf8').then(JSON.parse).catch(() => {}));
for (;;) {
const resp = await fetch(url, fetch_options);
if (resp.status !== 200) {
console.log(url, fetch_options);
if (resp.status === 200) {
return resp;
} else if (resp.status >= 500) {
console.log(`Got ${resp.status} - retrying: ${url}`);
await new Promise((res) => setTimeout(res, 5000));
} else {
console.log(url, resp.status, JSON.stringify(await resp.text()));
throw resp;
}
return resp;
}
};
export const get = (url, params) => {