mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
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>
This commit is contained in:
parent
1183dbf0b7
commit
f2b0ae119a
5 changed files with 52 additions and 31 deletions
|
|
@ -1,4 +1,5 @@
|
|||
{
|
||||
"data": {
|
||||
"data": {
|
||||
"user": null
|
||||
},
|
||||
|
|
@ -17,4 +18,5 @@
|
|||
"message": "Could not resolve to a User with the login of 'alekinho'."
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@
|
|||
},
|
||||
"rate_limit": {
|
||||
"data": {
|
||||
"data": {
|
||||
"user": null
|
||||
},
|
||||
"errors": [
|
||||
{
|
||||
"type": "RATE_LIMITED",
|
||||
|
|
|
|||
|
|
@ -28,14 +28,26 @@ stub(
|
|||
new Promise((resolve) => {
|
||||
resolve(successGithubResponseMock.default);
|
||||
}),
|
||||
// // Should get data in second Retry
|
||||
// Should throw NOT FOUND (requestUserInfo makes 4 API calls: repository, activity, issue, pullRequest)
|
||||
// Each call makes 2 attempts (one per token), so 8 promises total
|
||||
new Promise((resolve) => {
|
||||
resolve(rateLimitMock.default.rate_limit);
|
||||
resolve(notFoundGithubResponseMock.default);
|
||||
}),
|
||||
new Promise((resolve) => {
|
||||
resolve(successGithubResponseMock.default);
|
||||
resolve(notFoundGithubResponseMock.default);
|
||||
}),
|
||||
new Promise((resolve) => {
|
||||
resolve(notFoundGithubResponseMock.default);
|
||||
}),
|
||||
new Promise((resolve) => {
|
||||
resolve(notFoundGithubResponseMock.default);
|
||||
}),
|
||||
new Promise((resolve) => {
|
||||
resolve(notFoundGithubResponseMock.default);
|
||||
}),
|
||||
new Promise((resolve) => {
|
||||
resolve(notFoundGithubResponseMock.default);
|
||||
}),
|
||||
// Should throw NOT FOUND
|
||||
new Promise((resolve) => {
|
||||
resolve(notFoundGithubResponseMock.default);
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { soxa } from "../../deps.ts";
|
||||
import {
|
||||
EServiceKindError,
|
||||
GithubError,
|
||||
GithubErrorResponse,
|
||||
GithubExceedError,
|
||||
QueryDefaultResponse,
|
||||
|
|
@ -24,26 +25,28 @@ export async function requestGithubData<T = unknown>(
|
|||
return responseData.data.user;
|
||||
}
|
||||
|
||||
throw handleError(
|
||||
responseData as unknown as GithubErrorResponse | GithubExceedError,
|
||||
);
|
||||
throw handleError(responseData);
|
||||
}
|
||||
|
||||
function handleError(
|
||||
reponseErrors: GithubErrorResponse | GithubExceedError,
|
||||
responseData: {
|
||||
data?: unknown;
|
||||
errors?: GithubError[];
|
||||
message?: string;
|
||||
documentation_url?: string;
|
||||
},
|
||||
): ServiceError {
|
||||
let isRateLimitExceeded = false;
|
||||
const arrayErrors = (reponseErrors as GithubErrorResponse)?.errors || [];
|
||||
const objectError = (reponseErrors as GithubExceedError) || {};
|
||||
const arrayErrors = responseData?.errors || [];
|
||||
|
||||
if (Array.isArray(arrayErrors)) {
|
||||
if (Array.isArray(arrayErrors) && arrayErrors.length > 0) {
|
||||
isRateLimitExceeded = arrayErrors.some((error) =>
|
||||
error.type.includes(EServiceKindError.RATE_LIMIT)
|
||||
);
|
||||
}
|
||||
|
||||
if (objectError?.message) {
|
||||
isRateLimitExceeded = objectError?.message.includes(
|
||||
if (responseData?.message) {
|
||||
isRateLimitExceeded = responseData.message.toLowerCase().includes(
|
||||
"rate limit",
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ export type GithubExceedError = {
|
|||
export type QueryDefaultResponse<T = unknown> = {
|
||||
data: {
|
||||
data: T;
|
||||
errors?: GithubErrorResponse;
|
||||
errors?: GithubError[];
|
||||
message?: string;
|
||||
documentation_url?: string;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue