Skip to content
Closed
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
12 changes: 2 additions & 10 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,8 @@ async function graphql<T>(query: string, token?: string, variables?: Record<stri
body: JSON.stringify(body),
next: { revalidate: 300 },
});
if (res.status === 403) {
const resetHeader = res.headers.get("X-RateLimit-Reset");
const resetTimestamp = resetHeader ? parseInt(resetHeader, 10) : Math.floor(Date.now() / 1000) + 3600;
throw new RateLimitError(resetTimestamp);
}
if (!res.ok) {
const body = await res.text().catch(() => "Unknown error");
throw new GitHubApiError(body, res.status);
}
const json = (await res.json()) as { data?: T; errors?: { message: string }[] };

const json = await handleResponse<{ data?: T; errors?: { message: string }[] }>(res);
Comment on lines +67 to +68

Choose a reason for hiding this comment

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

high

This refactoring to use the shared handleResponse function introduces incorrect error handling for GraphQL calls.

The handleResponse function is designed to throw a UserNotFoundError on an HTTP 404 status, which is appropriate for REST API endpoints that fetch a specific resource (e.g., /users/:username).

However, for the GitHub GraphQL API, an HTTP 404 status indicates a problem with the endpoint itself, not that a user or other entity was not found. A "not found" error in GraphQL is typically returned with an HTTP 200 OK status and an errors array in the JSON payload, which is correctly handled further down in this function.

With this change, a legitimate endpoint error (like a typo in the GraphQL URL or a proxy issue) would be incorrectly reported as a UserNotFoundError, masking the true nature of the problem. The previous implementation correctly treated any non-OK response, including a 404, as a generic GitHubApiError.

To fix this, I recommend restoring the original error handling logic within the graphql function. This is the safest approach until handleResponse can be refactored to be more flexible for different API call types.

  if (res.status === 403) {
    const resetHeader = res.headers.get("X-RateLimit-Reset");
    const resetTimestamp = resetHeader ? parseInt(resetHeader, 10) : Math.floor(Date.now() / 1000) + 3600;
    throw new RateLimitError(resetTimestamp);
  }
  if (!res.ok) {
    const body = await res.text().catch(() => "Unknown error");
    throw new GitHubApiError(body, res.status);
  }
  const json = (await res.json()) as { data?: T; errors?: { message: string }[] };

if (json.errors && json.errors.length > 0) {
throw new GitHubApiError(json.errors[0].message, 422);
}
Expand Down
Loading