Skip to content

Commit

Permalink
fix: make graphql error retries actually work (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Feb 18, 2023
1 parent 194d6fc commit f29b398
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/error-request.ts
@@ -1,6 +1,6 @@
// @ts-ignore

export async function errorRequest(octokit, state, error, options) {
export async function errorRequest(state, octokit, error, options) {
if (!error.request || !error.request.request) {
// address https://github.com/octokit/plugin-retry.js/issues/8
throw error;
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -18,8 +18,8 @@ export function retry(octokit: Octokit, octokitOptions: any) {
);

if (state.enabled) {
octokit.hook.error("request", errorRequest.bind(null, octokit, state));
octokit.hook.wrap("request", wrapRequest.bind(null, state));
octokit.hook.error("request", errorRequest.bind(null, state, octokit));
octokit.hook.wrap("request", wrapRequest.bind(null, state, octokit));
}

return {
Expand Down
19 changes: 11 additions & 8 deletions src/wrap-request.ts
@@ -1,12 +1,11 @@
// @ts-ignore
// @ts-nocheck
import Bottleneck from "bottleneck/light";
import { RequestError } from "@octokit/request-error";
import { errorRequest } from "./error-request";

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

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

return limiter.schedule(
requestWithGraphqlErrorHandling.bind(null, request),
requestWithGraphqlErrorHandling.bind(null, state, octokit, request),
options
);
}

// @ts-ignore
async function requestWithGraphqlErrorHandling(request, options) {
async function requestWithGraphqlErrorHandling(
state,
octokit,
request,
options
) {
const response = await request(request, options);

if (
Expand All @@ -41,7 +44,7 @@ async function requestWithGraphqlErrorHandling(request, options) {
request: options,
response,
});
throw error;
return errorRequest(state, octokit, error, options);
}

return response;
Expand Down
4 changes: 2 additions & 2 deletions test/retry.test.ts
Expand Up @@ -426,7 +426,7 @@ describe("errorRequest", function () {
delete error.request;

try {
await errorRequest(octokit, state, error, errorOptions);
await errorRequest(state, octokit, error, errorOptions);
expect(1).not.toBe(1);
} catch (e: any) {
expect(e).toBe(error);
Expand Down Expand Up @@ -454,7 +454,7 @@ describe("errorRequest", function () {
const error = new RequestError("Internal server error", 500, errorOptions);

try {
await errorRequest(octokit, state, error, errorOptions);
await errorRequest(state, octokit, error, errorOptions);
expect(1).not.toBe(1);
} catch (e: any) {
expect(e.request.retries).toBe(5);
Expand Down

0 comments on commit f29b398

Please sign in to comment.