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

API rate limit error status can be 403 #1072

Merged
merged 1 commit into from
Jun 26, 2022
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
15 changes: 15 additions & 0 deletions lib/Github/HttpClient/Plugin/GithubExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
throw new SsoRequiredException($url);
}

$remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining');
if ((403 === $response->getStatusCode()) && null !== $remaining && 1 > $remaining && isset($content['message']) && (0 === strpos($content['message'], 'API rate limit exceeded'))) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');

throw new ApiLimitExceedException($limit, $reset);
}

$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
if ((403 === $response->getStatusCode()) && 0 < $reset && isset($content['message']) && (0 === strpos($content['message'], 'You have exceeded a secondary rate limit.'))) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');

throw new ApiLimitExceedException($limit, $reset);
}

throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
});
}
Expand Down
34 changes: 34 additions & 0 deletions test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,40 @@ public static function responseProvider()
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Rate Limit Exceeded via 403 status' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-RateLimit-Remaining' => 0,
'X-RateLimit-Limit' => 5000,
'X-RateLimit-Reset' => 1609245810,
],
json_encode(
[
'message' => 'API rate limit exceeded for installation ID xxxxxxx.',
]
)
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Secondary Rate Limit Exceeded via 403 status' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-RateLimit-Remaining' => 100,
'X-RateLimit-Limit' => 5000,
'X-RateLimit-Reset' => 1609245810,
],
json_encode(
[
'message' => 'You have exceeded a secondary rate limit. Please wait a few minutes before you try again.',
]
)
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Two Factor Authentication Required' => [
'response' => new Response(
401,
Expand Down