-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwrap-request.ts
51 lines (44 loc) · 1.38 KB
/
wrap-request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// @ts-nocheck
import Bottleneck from "bottleneck/light.js";
import { RequestError } from "@octokit/request-error";
import { errorRequest } from "./error-request.js";
export async function wrapRequest(state, octokit, request, options) {
const limiter = new Bottleneck();
limiter.on("failed", function (error, info) {
const maxRetries = ~~error.request.request.retries;
const after = ~~error.request.request.retryAfter;
options.request.retryCount = info.retryCount + 1;
if (maxRetries > info.retryCount) {
// Returning a number instructs the limiter to retry
// the request after that number of milliseconds have passed
return after * state.retryAfterBaseValue;
}
});
return limiter.schedule(
requestWithGraphqlErrorHandling.bind(null, state, octokit, request),
options,
);
}
async function requestWithGraphqlErrorHandling(
state,
octokit,
request,
options,
) {
const response = await request(request, options);
if (
response.data &&
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,
});
return errorRequest(state, octokit, error, options);
}
return response;
}