diff options
| author | Jack Nagel | 2014-02-16 22:24:33 -0500 |
|---|---|---|
| committer | Jack Nagel | 2014-02-16 23:19:09 -0500 |
| commit | 97e111d003bf584d76ab082c66505890ea6bfada (patch) | |
| tree | f4e2199c8f3313a8f8603b2210660d2b19909bf1 /Library | |
| parent | 1f4a261bd36febbd6b73b27c59b289a0788bd719 (diff) | |
| download | homebrew-97e111d003bf584d76ab082c66505890ea6bfada.tar.bz2 | |
Move error text and helper into error class
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/utils.rb | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 1f305a3b8..4bf9dbcc4 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -252,10 +252,28 @@ module GitHub extend self ISSUES_URI = URI.parse("https://api.github.com/search/issues") Error = Class.new(RuntimeError) - RateLimitExceededError = Class.new(Error) HTTPNotFoundError = 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 # This is a no-op if the user is opting out of using the GitHub API. return if ENV['HOMEBREW_NO_GITHUB_API'] @@ -282,13 +300,8 @@ module GitHub extend self def handle_api_error(e) if e.io.meta["x-ratelimit-remaining"].to_i <= 0 reset = e.io.meta.fetch("x-ratelimit-reset").to_i - - raise RateLimitExceededError, <<-EOS.undent, e.backtrace - 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 + error = Utils::JSON.load(e.io.read)["message"] + raise RateLimitExceededError.new(reset, error) end case e.io.status.first @@ -301,14 +314,6 @@ module GitHub extend self 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={}) uri = ISSUES_URI.dup uri.query = build_query_string(query, qualifiers) |
