Skip to content

Commit

Permalink
feat: retry "Something went wrong" GraphQL error (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jan 25, 2023
1 parent 659b0f4 commit f10aa37
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/wrap-request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-ignore
import Bottleneck from "bottleneck/light";
import { RequestError } from "@octokit/request-error";

// @ts-ignore
export async function wrapRequest(state, request, options) {
Expand All @@ -18,5 +19,29 @@ export async function wrapRequest(state, request, options) {
}
});

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

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

if (
response.data.errors &&
/Something went wrong while executing your query/.test(
response.data.errors[0].message
)
) {
// simulate 500 request error for retry handling
const error = new RequestError(response.data.errors[0].message, 500, {
request: options,
response,
});
throw error;
}

return response;
}
114 changes: 114 additions & 0 deletions test/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,120 @@ describe("Automatic Retries", function () {

expect(caught).toEqual(testStatuses.length);
});

it('Should retry "Something went wrong" GraphQL error', async function () {
const octokit = new TestOctokit();

const result = await octokit.graphql({
query: `query {
viewer {
login
}
}`,
request: {
responses: [
{
status: 200,
headers: {},
data: {
errors: [
{
message:
// the `0000:0000:0000000:0000000:00000000` part is variable, it's the request ID provided by GitHub
"Something went wrong while executing your query. Please include `0000:0000:0000000:0000000:00000000` when reporting this issue.",
},
],
},
},
{
status: 200,
headers: {},
data: {
data: {
viewer: {
login: "gr2m",
},
},
},
},
],
retries: 1,
},
});

expect(result).toStrictEqual({
viewer: {
login: "gr2m",
},
});
expect(octokit.__requestLog).toStrictEqual([
"START POST /graphql",
"END POST /graphql",
"START POST /graphql",
"END POST /graphql",
]);

expect(
octokit.__requestTimings[1] - octokit.__requestTimings[0]
).toBeLessThan(20);
});

it('Should not retry non-"Something went wrong" GraphQL errors', async function () {
const octokit = new TestOctokit();

try {
await octokit.graphql({
query: `query {
viewer {
login
}
}`,
request: {
responses: [
{
status: 200,
headers: {},
data: {
errors: [
{
message:
"Something that cannot be fixed with a request retry",
},
],
},
},
{
status: 200,
headers: {},
data: {
data: {
viewer: {
login: "gr2m",
},
},
},
},
],
retries: 1,
},
});
throw new Error("Should not reach this point");
} catch (error: any) {
expect(error.name).toEqual("GraphqlResponseError");
expect(error.message).toContain(
"Something that cannot be fixed with a request retry"
);
}

expect(octokit.__requestLog).toStrictEqual([
"START POST /graphql",
"END POST /graphql",
]);

expect(
octokit.__requestTimings[1] - octokit.__requestTimings[0]
).toBeLessThan(20);
});
});

describe("errorRequest", function () {
Expand Down

0 comments on commit f10aa37

Please sign in to comment.