Skip to content

Commit f2b0ae1

Browse files
Copilotryo-ma
andauthored
Fix mock data structure for rate limit and NOT_FOUND error handling (#399)
* Initial plan * Fix mock data structure and error handling for rate limit tests Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Minor cleanup: reorder handleError parameter fields for clarity Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Address code review feedback: remove unsafe non-null assertion and improve variable naming Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Fix test mock setup: remove mock calls for commented-out test and add missing mocks for requestUserInfo Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Clarify comment about retry attempts in test mock setup Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com>
1 parent 1183dbf commit f2b0ae1

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
{
22
"data": {
3-
"user": null
4-
},
5-
"errors": [
6-
{
7-
"type": "NOT_FOUND",
8-
"path": [
9-
"user"
10-
],
11-
"locations": [
12-
{
13-
"line": 2,
14-
"column": 5
15-
}
16-
],
17-
"message": "Could not resolve to a User with the login of 'alekinho'."
18-
}
19-
]
3+
"data": {
4+
"user": null
5+
},
6+
"errors": [
7+
{
8+
"type": "NOT_FOUND",
9+
"path": [
10+
"user"
11+
],
12+
"locations": [
13+
{
14+
"line": 2,
15+
"column": 5
16+
}
17+
],
18+
"message": "Could not resolve to a User with the login of 'alekinho'."
19+
}
20+
]
21+
}
2022
}

src/Services/__mocks__/rateLimitMock.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
},
88
"rate_limit": {
99
"data": {
10+
"data": {
11+
"user": null
12+
},
1013
"errors": [
1114
{
1215
"type": "RATE_LIMITED",

src/Services/__tests__/githubApiService.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,26 @@ stub(
2828
new Promise((resolve) => {
2929
resolve(successGithubResponseMock.default);
3030
}),
31-
// // Should get data in second Retry
31+
// Should throw NOT FOUND (requestUserInfo makes 4 API calls: repository, activity, issue, pullRequest)
32+
// Each call makes 2 attempts (one per token), so 8 promises total
3233
new Promise((resolve) => {
33-
resolve(rateLimitMock.default.rate_limit);
34+
resolve(notFoundGithubResponseMock.default);
3435
}),
3536
new Promise((resolve) => {
36-
resolve(successGithubResponseMock.default);
37+
resolve(notFoundGithubResponseMock.default);
38+
}),
39+
new Promise((resolve) => {
40+
resolve(notFoundGithubResponseMock.default);
41+
}),
42+
new Promise((resolve) => {
43+
resolve(notFoundGithubResponseMock.default);
44+
}),
45+
new Promise((resolve) => {
46+
resolve(notFoundGithubResponseMock.default);
47+
}),
48+
new Promise((resolve) => {
49+
resolve(notFoundGithubResponseMock.default);
3750
}),
38-
// Should throw NOT FOUND
3951
new Promise((resolve) => {
4052
resolve(notFoundGithubResponseMock.default);
4153
}),

src/Services/request.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { soxa } from "../../deps.ts";
22
import {
33
EServiceKindError,
4+
GithubError,
45
GithubErrorResponse,
56
GithubExceedError,
67
QueryDefaultResponse,
@@ -24,26 +25,28 @@ export async function requestGithubData<T = unknown>(
2425
return responseData.data.user;
2526
}
2627

27-
throw handleError(
28-
responseData as unknown as GithubErrorResponse | GithubExceedError,
29-
);
28+
throw handleError(responseData);
3029
}
3130

3231
function handleError(
33-
reponseErrors: GithubErrorResponse | GithubExceedError,
32+
responseData: {
33+
data?: unknown;
34+
errors?: GithubError[];
35+
message?: string;
36+
documentation_url?: string;
37+
},
3438
): ServiceError {
3539
let isRateLimitExceeded = false;
36-
const arrayErrors = (reponseErrors as GithubErrorResponse)?.errors || [];
37-
const objectError = (reponseErrors as GithubExceedError) || {};
40+
const arrayErrors = responseData?.errors || [];
3841

39-
if (Array.isArray(arrayErrors)) {
42+
if (Array.isArray(arrayErrors) && arrayErrors.length > 0) {
4043
isRateLimitExceeded = arrayErrors.some((error) =>
4144
error.type.includes(EServiceKindError.RATE_LIMIT)
4245
);
4346
}
4447

45-
if (objectError?.message) {
46-
isRateLimitExceeded = objectError?.message.includes(
48+
if (responseData?.message) {
49+
isRateLimitExceeded = responseData.message.toLowerCase().includes(
4750
"rate limit",
4851
);
4952
}

src/Types/Request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export type GithubExceedError = {
1515
export type QueryDefaultResponse<T = unknown> = {
1616
data: {
1717
data: T;
18-
errors?: GithubErrorResponse;
18+
errors?: GithubError[];
1919
message?: string;
20+
documentation_url?: string;
2021
};
2122
};

0 commit comments

Comments
 (0)