Skip to content

Commit

Permalink
Merge pull request #1382 from jasonopslevel/correctly_raise_secondary…
Browse files Browse the repository at this point in the history
…_rate_limit

Correctly raise Octokit::TooManyRequests when hitting secondary rate limit
  • Loading branch information
nickfloyd authored May 24, 2022
2 parents 519c913 + 87212aa commit 6fb1139
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/octokit/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def self.error_for_401(headers)
def self.error_for_403(body)
if body =~ /rate limit exceeded/i
Octokit::TooManyRequests
elsif body =~ /exceeded a secondary rate limit/i
Octokit::TooManyRequests
elsif body =~ /login attempts exceeded/i
Octokit::TooManyLoginAttempts
elsif body =~ /returns blobs up to [0-9]+ MB/i
Expand Down
41 changes: 41 additions & 0 deletions spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,14 @@
:body => {:message => "API rate limit exceeded"}.to_json
expect { Octokit.get('/users/mojomobo') }.to raise_error Octokit::TooManyRequests

stub_get('/users/mojomobo').to_return \
:status => 403,
:headers => {
:content_type => "application/json",
},
:body => {:message => "You have exceeded a secondary rate limit."}.to_json
expect { Octokit.get('/users/mojomobo') }.to raise_error Octokit::TooManyRequests

stub_get('/user').to_return \
:status => 403,
:headers => {
Expand Down Expand Up @@ -1155,6 +1163,39 @@
end
end

it "returns context with default data when rate limit error occurs but headers are missing" do
stub_get('/user').to_return \
:status => 403,
:headers => {
:content_type => "application/json",
},
:body => {:message => "You have exceeded a secondary rate limit."}.to_json
begin
expect_any_instance_of(Faraday::Env).to receive(:headers).at_least(:once).and_return({})
Octokit.get('/user')
rescue Octokit::TooManyRequests => rate_limit_error
expect(rate_limit_error.context).to be_an_instance_of(Octokit::RateLimit)
end
end

it "returns context when non rate limit error occurs but rate limit headers are present" do
stub_get('/user').to_return \
:status => 403,
:headers => {
'content_type' => 'application/json'
},
:body => {:message => "You have exceeded a secondary rate limit."}.to_json
begin
rate_limit_headers = {'X-RateLimit-Limit' => 60, 'X-RateLimit-Remaining' => 42, 'X-RateLimit-Reset' => (Time.now + 60).to_i}
expect_any_instance_of(Faraday::Env).to receive(:headers).at_least(:once).and_return(rate_limit_headers)
Octokit.get('/user')
rescue Octokit::TooManyRequests => rate_limit_error
expect(rate_limit_error.context).to be_an_instance_of(Octokit::RateLimit)
expect(rate_limit_error.context.limit).to eql 60
expect(rate_limit_error.context.remaining).to eql 42
end
end

describe "module call shortcut" do
before do
Octokit.reset!
Expand Down

0 comments on commit 6fb1139

Please sign in to comment.