diff --git a/lib/fbe/octo.rb b/lib/fbe/octo.rb index ee0ce178..c268fa0e 100644 --- a/lib/fbe/octo.rb +++ b/lib/fbe/octo.rb @@ -170,10 +170,18 @@ def print_trace!(all: false, max: 5) end .take(max) .join("\n") + quota = + begin + "#{@origin.rate_limit!.remaining} quota left" + rescue Octokit::ServerError, Octokit::Unauthorized, Octokit::Forbidden, + Faraday::ConnectionFailed, Faraday::TimeoutError => e + @loog.warn("Failed to check GitHub API quota for the trace: #{e.message}") + 'quota unknown' + end @loog.info( "GitHub API trace (#{grouped.count} URLs vs #{shown.count} requests, " \ "#{@trace.count - shown.count} fast ones skipped, " \ - "#{@origin.rate_limit!.remaining} quota left):\n#{message}" + "#{quota}):\n#{message}" ) @trace.clear end diff --git a/test/fbe/test_octo.rb b/test/fbe/test_octo.rb index 9efa8b8a..4db41373 100644 --- a/test/fbe/test_octo.rb +++ b/test/fbe/test_octo.rb @@ -1001,6 +1001,24 @@ def test_print_trace_counts_only_printed_requests assert_operator(shown + skipped, :>=, total) end + def test_print_trace_when_quota_can_not_be_read + loog = Loog::Buffer.new + WebMock.disable_net_connect! + stub_request(:get, 'https://api.github.com/rate_limit').to_return( + body: '{"rate":{"remaining":222}}', headers: { 'X-RateLimit-Remaining' => '222' } + ) + stub_request(:get, 'https://api.github.com/repos/foo/bar').to_return( + body: '{"id":456,"full_name":"foo/bar"}', headers: { 'X-RateLimit-Remaining' => '220' } + ) + octo = Fbe.octo(loog:, global: {}, options: Judges::Options.new) + octo.repository('foo/bar') + octo.instance_variable_get(:@origin).define_singleton_method(:rate_limit!) do + raise(Faraday::ConnectionFailed, 'GitHub is down') + end + octo.print_trace!(all: true) + assert_includes(loog.to_s, 'quota unknown') + end + def test_print_trace loog = Loog::Buffer.new WebMock.disable_net_connect!