Skip to content

Commit f29b398

Browse files
authored
fix: make graphql error retries actually work (#403)
1 parent 194d6fc commit f29b398

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/error-request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-ignore
22

3-
export async function errorRequest(octokit, state, error, options) {
3+
export async function errorRequest(state, octokit, error, options) {
44
if (!error.request || !error.request.request) {
55
// address https://github.com/octokit/plugin-retry.js/issues/8
66
throw error;

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export function retry(octokit: Octokit, octokitOptions: any) {
1818
);
1919

2020
if (state.enabled) {
21-
octokit.hook.error("request", errorRequest.bind(null, octokit, state));
22-
octokit.hook.wrap("request", wrapRequest.bind(null, state));
21+
octokit.hook.error("request", errorRequest.bind(null, state, octokit));
22+
octokit.hook.wrap("request", wrapRequest.bind(null, state, octokit));
2323
}
2424

2525
return {

src/wrap-request.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// @ts-ignore
1+
// @ts-nocheck
22
import Bottleneck from "bottleneck/light";
33
import { RequestError } from "@octokit/request-error";
4+
import { errorRequest } from "./error-request";
45

5-
// @ts-ignore
6-
export async function wrapRequest(state, request, options) {
6+
export async function wrapRequest(state, octokit, request, options) {
77
const limiter = new Bottleneck();
88

9-
// @ts-ignore
109
limiter.on("failed", function (error, info) {
1110
const maxRetries = ~~error.request.request.retries;
1211
const after = ~~error.request.request.retryAfter;
@@ -20,13 +19,17 @@ export async function wrapRequest(state, request, options) {
2019
});
2120

2221
return limiter.schedule(
23-
requestWithGraphqlErrorHandling.bind(null, request),
22+
requestWithGraphqlErrorHandling.bind(null, state, octokit, request),
2423
options
2524
);
2625
}
2726

28-
// @ts-ignore
29-
async function requestWithGraphqlErrorHandling(request, options) {
27+
async function requestWithGraphqlErrorHandling(
28+
state,
29+
octokit,
30+
request,
31+
options
32+
) {
3033
const response = await request(request, options);
3134

3235
if (
@@ -41,7 +44,7 @@ async function requestWithGraphqlErrorHandling(request, options) {
4144
request: options,
4245
response,
4346
});
44-
throw error;
47+
return errorRequest(state, octokit, error, options);
4548
}
4649

4750
return response;

test/retry.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ describe("errorRequest", function () {
426426
delete error.request;
427427

428428
try {
429-
await errorRequest(octokit, state, error, errorOptions);
429+
await errorRequest(state, octokit, error, errorOptions);
430430
expect(1).not.toBe(1);
431431
} catch (e: any) {
432432
expect(e).toBe(error);
@@ -454,7 +454,7 @@ describe("errorRequest", function () {
454454
const error = new RequestError("Internal server error", 500, errorOptions);
455455

456456
try {
457-
await errorRequest(octokit, state, error, errorOptions);
457+
await errorRequest(state, octokit, error, errorOptions);
458458
expect(1).not.toBe(1);
459459
} catch (e: any) {
460460
expect(e.request.retries).toBe(5);

0 commit comments

Comments
 (0)