Skip to content

Commit c1fdbd6

Browse files
authored
fix: Fetch with retry not actually retrying (#59)
Fix fetchWithRetry not actually retrying.
1 parent 233f870 commit c1fdbd6

File tree

6 files changed

+67
-5
lines changed

6 files changed

+67
-5
lines changed

.changeset/fluffy-candles-switch.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@codecov/bundler-plugin-core": patch
3+
"@codecov/rollup-plugin": patch
4+
"@codecov/vite-plugin": patch
5+
"@codecov/webpack-plugin": patch
6+
---
7+
8+
Fix retry when fetching by throwing custom error if response is not okay
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class BadResponseError extends Error {
2+
constructor(msg: string) {
3+
super(msg);
4+
}
5+
}

packages/bundler-plugin-core/src/utils/__tests__/fetchWithRetry.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface SetupArgs {
2424
data?: object;
2525
retryCount?: number;
2626
sendError?: boolean;
27+
failFetch?: boolean;
2728
}
2829

2930
describe("fetchWithRetry", () => {
@@ -37,17 +38,23 @@ describe("fetchWithRetry", () => {
3738
status = 200,
3839
data = {},
3940
sendError = false,
41+
failFetch = false,
4042
retryCount = 0,
4143
}: SetupArgs) {
4244
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => null);
4345

4446
server.use(
4547
http.all("http://localhost", ({}) => {
46-
if (retryCount === 0 && !sendError) {
48+
if (retryCount === 0 && !sendError && !failFetch) {
4749
return HttpResponse.json(data, { status });
4850
}
51+
52+
if (sendError && !failFetch) {
53+
return HttpResponse.error();
54+
}
55+
4956
retryCount -= 1;
50-
return HttpResponse.error();
57+
return new HttpResponse("not found", { status: 404 });
5158
}),
5259
);
5360
}
@@ -89,6 +96,24 @@ describe("fetchWithRetry", () => {
8996
});
9097

9198
describe("retry count exceeds limit", () => {
99+
it("returns the response", async () => {
100+
setup({
101+
data: { url: "http://example.com" },
102+
retryCount: 2,
103+
failFetch: true,
104+
});
105+
106+
const response = await fetchWithRetry({
107+
url: "http://localhost",
108+
requestData: {},
109+
retryCount: 1,
110+
});
111+
112+
expect(response.status).toBe(404);
113+
});
114+
});
115+
116+
describe("when the fetch throws an error", () => {
92117
it("throws an error", async () => {
93118
setup({
94119
data: { url: "http://example.com" },

packages/bundler-plugin-core/src/utils/__tests__/getPreSignedURL.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ describe("getPreSignedURL", () => {
6565
serviceParams: {
6666
commit: "123",
6767
},
68+
retryCount: 0,
6869
});
6970

7071
expect(url).toEqual("http://example.com");
@@ -87,6 +88,7 @@ describe("getPreSignedURL", () => {
8788
serviceParams: {
8889
commit: "123",
8990
},
91+
retryCount: 0,
9092
});
9193
} catch (e) {
9294
error = e;
@@ -116,6 +118,7 @@ describe("getPreSignedURL", () => {
116118
serviceParams: {
117119
commit: "123",
118120
},
121+
retryCount: 0,
119122
});
120123
} catch (e) {
121124
error = e;
@@ -141,6 +144,7 @@ describe("getPreSignedURL", () => {
141144
serviceParams: {
142145
commit: "123",
143146
},
147+
retryCount: 0,
144148
});
145149
} catch (e) {
146150
error = e;
@@ -176,6 +180,7 @@ describe("getPreSignedURL", () => {
176180
serviceParams: {
177181
commit: "123",
178182
},
183+
retryCount: 0,
179184
});
180185
} catch (e) {
181186
error = e;
@@ -201,6 +206,7 @@ describe("getPreSignedURL", () => {
201206
serviceParams: {
202207
commit: "123",
203208
},
209+
retryCount: 0,
204210
});
205211
} catch (e) {
206212
error = e;
@@ -236,6 +242,7 @@ describe("getPreSignedURL", () => {
236242
serviceParams: {
237243
commit: "123",
238244
},
245+
retryCount: 0,
239246
});
240247
} catch (e) {
241248
error = e;

packages/bundler-plugin-core/src/utils/__tests__/uploadStats.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe("uploadStats", () => {
6262
const data = await uploadStats({
6363
message: "cool-message",
6464
preSignedUrl: "http://localhost/upload/stats/",
65+
retryCount: 0,
6566
});
6667

6768
expect(data).toBeTruthy();
@@ -75,7 +76,11 @@ describe("uploadStats", () => {
7576

7677
let error;
7778
try {
78-
await uploadStats({ message: "cool-message", preSignedUrl: "" });
79+
await uploadStats({
80+
message: "cool-message",
81+
preSignedUrl: "",
82+
retryCount: 1,
83+
});
7984
} catch (e) {
8085
error = e;
8186
}
@@ -93,6 +98,7 @@ describe("uploadStats", () => {
9398
await uploadStats({
9499
message: "cool-message",
95100
preSignedUrl: "http://localhost/upload/stats/",
101+
retryCount: 0,
96102
});
97103
} catch (e) {
98104
error = e;
@@ -111,6 +117,7 @@ describe("uploadStats", () => {
111117
await uploadStats({
112118
message: "cool-message",
113119
preSignedUrl: "http://localhost/upload/stats/",
120+
retryCount: 0,
114121
});
115122
} catch (e) {
116123
error = e;

packages/bundler-plugin-core/src/utils/fetchWithRetry.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BadResponseError } from "../errors/BadResponseError";
12
import { DEFAULT_RETRY_DELAY } from "./constants";
23
import { delay } from "./delay";
34
import { debug, red } from "./logging";
@@ -22,13 +23,22 @@ export const fetchWithRetry = async ({
2223
debug(`Attempting to fetch ${name}, attempt: ${i}`);
2324
await delay(DEFAULT_RETRY_DELAY * i);
2425
response = await fetch(url, requestData);
25-
break;
26+
27+
if (!response.ok) {
28+
throw new BadResponseError("Response not ok");
29+
}
2630
} catch (err) {
2731
debug(`${name} fetch attempt ${i} failed`);
2832
const isLastAttempt = i + 1 === retryCount;
33+
2934
if (isLastAttempt) {
3035
red(`${name} failed after ${i} attempts`);
31-
throw err;
36+
37+
if (!(err instanceof BadResponseError)) {
38+
console.debug("ahh");
39+
throw err;
40+
}
41+
return response;
3242
}
3343
}
3444
}

0 commit comments

Comments
 (0)