Skip to content

fix: make graphql error retries actually work #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/error-request.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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