From 3c73e11212053f675d8ad3d7554fd71afdd6b51f Mon Sep 17 00:00:00 2001 From: stanleyphu Date: Wed, 20 Dec 2023 11:05:20 -0800 Subject: [PATCH] Retry on more 50X status codes --- src/HttpClient.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/HttpClient.ts b/src/HttpClient.ts index c4702a5..4b0013f 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -37,6 +37,7 @@ interface FetchRequestOptions { const MAX_RETRY_ATTEMPTS = 3; const BACKOFF_MULTIPLIER = 1.5; const MINIMUM_SLEEP_TIME = 500; +const RETRY_STATUS_CODES = [500, 502, 504]; const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) @@ -95,6 +96,11 @@ export default class ApiClient implements HttpClient { retryAttempts++; await sleep(this.getSleepTime(retryAttempts)); return makeRequest(); + + return setTimeout(async () => { + const response = await makeRequest(); + return new Promise((resolve) => resolve(response)); + }) } if (!response.ok) { @@ -116,7 +122,7 @@ export default class ApiClient implements HttpClient { return true; } - if (response?.status == 502) { + if (response != null && RETRY_STATUS_CODES.includes(response.status)) { return true; }