Move error text and helper into error class

This commit is contained in:
Jack Nagel 2014-02-16 22:24:33 -05:00
parent 24cbb4fd2e
commit 3cbb49930c

View File

@ -252,10 +252,28 @@ module GitHub extend self
ISSUES_URI = URI.parse("https://api.github.com/search/issues") ISSUES_URI = URI.parse("https://api.github.com/search/issues")
Error = Class.new(RuntimeError) Error = Class.new(RuntimeError)
RateLimitExceededError = Class.new(Error)
HTTPNotFoundError = Class.new(Error) HTTPNotFoundError = Class.new(Error)
AuthenticationFailedError = Class.new(Error) AuthenticationFailedError = Class.new(Error)
class RateLimitExceededError < Error
def initialize(reset, error)
super <<-EOS.undent
GitHub #{error}
Try again in #{pretty_ratelimit_reset(reset)}, or create an API token:
https://github.com/settings/applications
and then set HOMEBREW_GITHUB_API_TOKEN.
EOS
end
def pretty_ratelimit_reset(reset)
if (seconds = Time.at(reset) - Time.now) > 180
"%d minutes %d seconds" % [seconds / 60, seconds % 60]
else
"#{seconds} seconds"
end
end
end
def open url, headers={}, &block def open url, headers={}, &block
# This is a no-op if the user is opting out of using the GitHub API. # This is a no-op if the user is opting out of using the GitHub API.
return if ENV['HOMEBREW_NO_GITHUB_API'] return if ENV['HOMEBREW_NO_GITHUB_API']
@ -282,13 +300,8 @@ module GitHub extend self
def handle_api_error(e) def handle_api_error(e)
if e.io.meta["x-ratelimit-remaining"].to_i <= 0 if e.io.meta["x-ratelimit-remaining"].to_i <= 0
reset = e.io.meta.fetch("x-ratelimit-reset").to_i reset = e.io.meta.fetch("x-ratelimit-reset").to_i
error = Utils::JSON.load(e.io.read)["message"]
raise RateLimitExceededError, <<-EOS.undent, e.backtrace raise RateLimitExceededError.new(reset, error)
GitHub #{Utils::JSON.load(e.io.read)['message']}
Try again in #{pretty_ratelimit_reset(reset)}, or create an API token:
https://github.com/settings/applications
and then set HOMEBREW_GITHUB_API_TOKEN.
EOS
end end
case e.io.status.first case e.io.status.first
@ -301,14 +314,6 @@ module GitHub extend self
end end
end end
def pretty_ratelimit_reset(reset)
if (seconds = Time.at(reset) - Time.now) > 180
"%d minutes %d seconds" % [seconds / 60, seconds % 60]
else
"#{seconds} seconds"
end
end
def issues_matching(query, qualifiers={}) def issues_matching(query, qualifiers={})
uri = ISSUES_URI.dup uri = ISSUES_URI.dup
uri.query = build_query_string(query, qualifiers) uri.query = build_query_string(query, qualifiers)