Skip to content

Commit 28ca615

Browse files
update retry logic
1 parent 865c83e commit 28ca615

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

sdk/src/network-client.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,12 @@ class AleoNetworkClient {
138138
*/
139139
async fetchData<Type>(url = "/"): Promise<Type> {
140140
try {
141-
const raw = await retryWithBackoff(() => this.fetchRaw(url), {
142-
retryOnStatus: [500, 502, 503, 504],
143-
shouldRetry: (err) => {
144-
const msg = err?.message?.toLowerCase?.() || "";
145-
return msg.includes("network") || msg.includes("timeout") || msg.includes("503");
146-
}
147-
});
141+
const raw = await retryWithBackoff(() => this.fetchRaw(url));
148142
return parseJSON(raw);
149143
} catch (error) {
150144
throw new Error(`Error fetching data: ${error}`);
151145
}
152146
}
153-
154147

155148
/**
156149
* Fetches data from the Aleo network and returns it as an unparsed string.
@@ -163,17 +156,16 @@ class AleoNetworkClient {
163156
async fetchRaw(url = "/"): Promise<string> {
164157
try {
165158
return await retryWithBackoff(async () => {
166-
const response = await get(this.host + url, {
167-
headers: this.headers,
168-
});
169-
return await response.text();
170-
}, {
171-
retryOnStatus: [500, 502, 503, 504],
172-
shouldRetry: (err) => {
173-
const msg = err?.message?.toLowerCase?.() || "";
174-
return msg.includes("network") || msg.includes("timeout") || msg.includes("503");
175-
}
176-
});
159+
const response = await get(this.host + url, {
160+
headers: this.headers,
161+
});
162+
return await response.text();
163+
}, {
164+
shouldRetry: (err) => {
165+
const msg = err?.message?.toLowerCase?.() || "";
166+
return msg.includes("network") || msg.includes("timeout");
167+
}
168+
});
177169
} catch (error) {
178170
throw new Error(`Error fetching data: ${error}`);
179171
}

sdk/src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ type RetryOptions = {
6666
let retryable = false;
6767

6868
if (typeof error.status === "number") {
69-
retryable = retryOnStatus.includes(error.status);
69+
if (error.status >= 500) {
70+
retryable = true;
71+
} else if (error.status >= 400 && shouldRetry) {
72+
retryable = shouldRetry(error);
73+
}
7074
} else if (shouldRetry) {
7175
retryable = shouldRetry(error);
7276
}

0 commit comments

Comments
 (0)