retry requests returning 5xx after 5 seconds

master
Conduitry 2 weeks ago
parent 92fb64675a
commit 1fca797207

@ -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(() => {}));
const resp = await fetch(url, fetch_options);
if (resp.status !== 200) {
console.log(url, fetch_options);
throw resp;
for (;;) {
const resp = await fetch(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) => {

Loading…
Cancel
Save