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

Adding in handling for secondary rate limit exceptions #2473

Merged
merged 2 commits into from Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion Octokit.Tests/Http/ConnectionTests.cs
Expand Up @@ -180,7 +180,7 @@ public async Task ThrowsApiValidationExceptionFor422Response()
}

[Fact]
public async Task ThrowsRateLimitExceededExceptionForForbidderResponse()
Copy link

Choose a reason for hiding this comment

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

public async Task ThrowsRateLimitExceededExceptionForForbiddenResponse()
{
var httpClient = Substitute.For<IHttpClient>();
var response = CreateResponse(
Expand All @@ -202,6 +202,28 @@ public async Task ThrowsRateLimitExceededExceptionForForbidderResponse()
exception.Message);
}

[Fact]
public async Task ThrowsSecondaryRateLimitExceededExceptionForForbiddenResponse()
{
var httpClient = Substitute.For<IHttpClient>();
var response = CreateResponse(
HttpStatusCode.Forbidden,
"{\"message\":\"You have exceeded a secondary rate limit. Please wait a few minutes before you try again.\"}");

httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
var connection = new Connection(new ProductHeaderValue("OctokitTests"),
_exampleUri,
Substitute.For<ICredentialStore>(),
httpClient,
Substitute.For<IJsonSerializer>());

var exception = await Assert.ThrowsAsync<RateLimitExceededException>(
() => connection.GetResponse<string>(new Uri("endpoint", UriKind.Relative)));

Assert.Equal("You have exceeded a secondary rate limit. Please wait a few minutes before you try again.",
exception.Message);
}

[Fact]
public async Task ThrowsLoginAttemptsExceededExceptionForForbiddenResponse()
{
Expand Down
2 changes: 1 addition & 1 deletion Octokit/Http/Connection.cs
Expand Up @@ -719,7 +719,7 @@ static Exception GetExceptionForForbidden(IResponse response)
{
string body = response.Body as string ?? "";

if (body.Contains("rate limit exceeded"))
if (body.Contains("rate limit exceeded") || body.Contains("secondary rate limit"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it might be better throw a different kind of exception for a secondary rate limit. What do you think? I'm just thinking that it would make it easier to handle this specific case as a caller, without doing string matching.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I get where you are coming from, makes sense to me :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I’d just define a new exception and raise that. It should be a quick change, and then we can get this approved and merged ✅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've changed it to be it's own exception.

{
return new RateLimitExceededException(response);
}
Expand Down