Skip to content
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

feat: retry "Something went wrong" GraphQL error #396

Merged
merged 4 commits into from
Jan 25, 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
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;
}
Comment on lines +32 to +44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is a more elegant solution to this, but I couldn't think of any. This is a very specific handling of basically the 500 error status version of GraphQL queries.


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