Skip to content

Commit

Permalink
github: produce better curl error messages. (#441)
Browse files Browse the repository at this point in the history
* global: add RUBY_TWO global variable.

* test-bot: use RUBY_TWO global variable.

* github: produce better curl error messages.

If we don't know why curl has failed then ensure that the error messages
that it produced are included as part of the user output.
  • Loading branch information
MikeMcQuaid authored Jul 12, 2016
1 parent ed1d1e5 commit 23306ab
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
6 changes: 2 additions & 4 deletions Library/Homebrew/dev-cmd/test-bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,9 @@ def homebrew
@category = __method__
return if @skip_homebrew

ruby_two = RUBY_VERSION.split(".").first.to_i >= 2

if @tap.nil?
tests_args = []
if ruby_two
if RUBY_TWO
tests_args << "--official-cmd-taps"
tests_args << "--coverage" if ENV["TRAVIS"]
end
Expand Down Expand Up @@ -892,7 +890,7 @@ def sanitize_ARGV_and_ENV

ENV["HOMEBREW_DEVELOPER"] = "1"
ENV["HOMEBREW_SANDBOX"] = "1"
ENV["HOMEBREW_RUBY_MACHO"] = "1" if RUBY_VERSION.split(".").first.to_i >= 2
ENV["HOMEBREW_RUBY_MACHO"] = "1" if RUBY_TWO
ENV["HOMEBREW_NO_EMOJI"] = "1"
ENV["HOMEBREW_FAIL_LOG_LINES"] = "150"
ENV["HOMEBREW_EXPERIMENTAL_FILTER_FLAGS_ON_DEPS"] = "1"
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
end
RUBY_BIN = RUBY_PATH.dirname
RUBY_TWO = RUBY_VERSION.split(".").first.to_i >= 2

HOMEBREW_USER_AGENT_CURL = ENV["HOMEBREW_USER_AGENT_CURL"]
HOMEBREW_USER_AGENT_RUBY = "#{ENV["HOMEBREW_USER_AGENT"]} ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
Expand Down
14 changes: 12 additions & 2 deletions Library/Homebrew/utils/curl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "pathname"
require "open3"

def curl_args(extra_args=[])
curl = Pathname.new ENV["HOMEBREW_CURL"]
Expand All @@ -19,6 +20,15 @@ def curl(*args)
end

def curl_output(*args)
curl_args = curl_args(args) - ["--fail"]
Utils.popen_read_text(*curl_args)
curl_args = curl_args(args)
curl_args -= ["--fail"]
if RUBY_TWO
curl_args -= ["--silent"]
Open3.popen3(*curl_args) do |_, stdout, stderr, wait_thread|
[stdout.read, stderr.read, wait_thread.value]
end
else
output = Utils.popen_read_text(*curl_args)
[output, nil, $?]
end
end
11 changes: 6 additions & 5 deletions Library/Homebrew/utils/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def open(url, data=nil)

args += ["--dump-header", "#{headers_tmpfile.path}"]

output, _, http_code = curl_output(url.to_s, *args).rpartition("\n")
output, errors, status = curl_output(url.to_s, *args)
output, _, http_code = output.rpartition("\n")
output, _, http_code = output.rpartition("\n") if http_code == "000"
headers = headers_tmpfile.read
ensure
Expand All @@ -159,8 +160,8 @@ def open(url, data=nil)
end

begin
if !http_code.start_with?("2") && !$?.success?
raise_api_error(output, http_code, headers)
if !http_code.start_with?("2") && !status.success?
raise_api_error(output, errors, http_code, headers)
end
json = Utils::JSON.load output
if block_given?
Expand All @@ -173,7 +174,7 @@ def open(url, data=nil)
end
end

def raise_api_error(output, http_code, headers)
def raise_api_error(output, errors, http_code, headers)
meta = {}
headers.lines.each do |l|
key, _, value = l.delete(":").partition(" ")
Expand All @@ -197,7 +198,7 @@ def raise_api_error(output, http_code, headers)
raise HTTPNotFoundError, output
else
error = Utils::JSON.load(output)["message"] rescue nil
error ||= output
error ||= "curl failed! #{errors}"
raise Error, error
end
end
Expand Down

0 comments on commit 23306ab

Please sign in to comment.