Skip to content

Commit

Permalink
Refactor Gem::RemoteFetcher::FetchError.build back to its initializ…
Browse files Browse the repository at this point in the history
…e method
  • Loading branch information
daniel-niknam authored and deivid-rodriguez committed Aug 24, 2021
1 parent dba130c commit 21dcdd2
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 37 deletions.
35 changes: 13 additions & 22 deletions lib/rubygems/remote_fetcher.rb
Expand Up @@ -22,24 +22,15 @@ class Gem::RemoteFetcher
class FetchError < Gem::Exception
##
# The URI which was being accessed when the exception happened.
def self.build(message, uri)
original_uri = uri.dup
uri = Gem::PrintableUri.parse_uri(uri)

if uri.respond_to?(:original_password) && uri.original_password
message = message.sub(uri.original_password, 'REDACTED')
end

new(message, uri.to_s, original_uri)
end

attr_accessor :uri, :original_uri

def initialize(message, uri, original_uri = nil)
super message
def initialize(message, uri)
@original_uri = uri.dup
uri = Gem::PrintableUri.parse_uri(uri)

super(uri.valid_uri? && uri.original_password ? message.sub(uri.original_password, 'REDACTED') : message)

@uri = uri
@original_uri = original_uri ? original_uri : uri
@uri = uri.to_s
end

def to_s # :nodoc:
Expand Down Expand Up @@ -225,20 +216,20 @@ def fetch_http(uri, last_modified = nil, head = false, depth = 0)
head ? response : response.body
when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther,
Net::HTTPTemporaryRedirect then
raise FetchError.build('too many redirects', uri) if depth > 10
raise FetchError.new('too many redirects', uri) if depth > 10

unless location = response['Location']
raise FetchError.build("redirecting but no redirect location was given", uri)
raise FetchError.new("redirecting but no redirect location was given", uri)
end
location = Gem::UriParser.parse_uri location

if https?(uri) && !https?(location)
raise FetchError.build("redirecting to non-https resource: #{location}", uri)
raise FetchError.new("redirecting to non-https resource: #{location}", uri)
end

fetch_http(location, last_modified, head, depth + 1)
else
raise FetchError.build("bad response #{response.message} #{response.code}", uri)
raise FetchError.new("bad response #{response.message} #{response.code}", uri)
end
end

Expand All @@ -260,21 +251,21 @@ def fetch_path(uri, mtime = nil, head = false)
begin
data = Gem::Util.gunzip data
rescue Zlib::GzipFile::Error
raise FetchError.build("server did not return a valid file", uri)
raise FetchError.new("server did not return a valid file", uri)
end
end

data
rescue Timeout::Error, IOError, SocketError, SystemCallError,
*(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e
raise FetchError.build("#{e.class}: #{e}", uri)
raise FetchError.new("#{e.class}: #{e}", uri)
end

def fetch_s3(uri, mtime = nil, head = false)
begin
public_uri = s3_uri_signer(uri).sign
rescue Gem::S3URISigner::ConfigurationError, Gem::S3URISigner::InstanceProfileError => e
raise FetchError.build(e.message, "s3://#{uri.host}")
raise FetchError.new(e.message, "s3://#{uri.host}")
end
fetch_https public_uri, mtime, head
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rubygems/request.rb
Expand Up @@ -127,7 +127,7 @@ def connection_for(uri)
@connection_pool.checkout
rescue Gem::HAVE_OPENSSL ? OpenSSL::SSL::SSLError : Errno::EHOSTDOWN,
Errno::EHOSTDOWN => e
raise Gem::RemoteFetcher::FetchError.build(e.message, uri)
raise Gem::RemoteFetcher::FetchError.new(e.message, uri)
end

def fetch
Expand Down Expand Up @@ -228,14 +228,14 @@ def perform_request(request) # :nodoc:

reset connection

raise Gem::RemoteFetcher::FetchError.build('too many bad responses', @uri) if bad_response
raise Gem::RemoteFetcher::FetchError.new('too many bad responses', @uri) if bad_response

bad_response = true
retry
rescue Net::HTTPFatalError
verbose "fatal error"

raise Gem::RemoteFetcher::FetchError.build('fatal error', @uri)
raise Gem::RemoteFetcher::FetchError.new('fatal error', @uri)
# HACK work around EOFError bug in Net::HTTP
# NOTE Errno::ECONNABORTED raised a lot on Windows, and make impossible
# to install gems.
Expand All @@ -245,7 +245,7 @@ def perform_request(request) # :nodoc:
requests = @requests[connection.object_id]
verbose "connection reset after #{requests} requests, retrying"

raise Gem::RemoteFetcher::FetchError.build('too many connection resets', @uri) if retried
raise Gem::RemoteFetcher::FetchError.new('too many connection resets', @uri) if retried

reset connection

Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_commands_sources_command.rb
Expand Up @@ -182,7 +182,7 @@ def test_execute_add_nonexistent_source

uri = "http://beta-gems.example.com/specs.#{@marshal_version}.gz"
@fetcher.data[uri] = proc do
raise Gem::RemoteFetcher::FetchError.build('it died', uri)
raise Gem::RemoteFetcher::FetchError.new('it died', uri)
end

@cmd.handle_options %w[--add http://beta-gems.example.com]
Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_remote_fetcher.rb
Expand Up @@ -204,7 +204,7 @@ def self.fetch_path(arg, *rest)
@test_data
end

raise Gem::RemoteFetcher::FetchError.build("haha!", '')
raise Gem::RemoteFetcher::FetchError.new("haha!", '')
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/rubygems/test_gem_resolver_best_set.rb
Expand Up @@ -106,7 +106,7 @@ def test_replace_failed_api_set

error_uri = api_uri + 'a'

error = Gem::RemoteFetcher::FetchError.build 'bogus', error_uri
error = Gem::RemoteFetcher::FetchError.new 'bogus', error_uri

set.replace_failed_api_set error

Expand All @@ -124,7 +124,7 @@ def test_replace_failed_api_set_no_api_set

set.sets << index_set

error = Gem::RemoteFetcher::FetchError.build 'bogus', @gem_repo
error = Gem::RemoteFetcher::FetchError.new 'bogus', @gem_repo

e = assert_raise Gem::RemoteFetcher::FetchError do
set.replace_failed_api_set error
Expand All @@ -145,7 +145,7 @@ def test_replace_failed_api_set_uri_with_credentials

error_uri = api_uri + 'a'

error = Gem::RemoteFetcher::FetchError.build 'bogus', error_uri
error = Gem::RemoteFetcher::FetchError.new 'bogus', error_uri

set.replace_failed_api_set error

Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_spec_fetcher.rb
Expand Up @@ -144,7 +144,7 @@ def test_spec_for_dependency_mismatched_platform
def test_spec_for_dependency_bad_fetch_spec
src = Gem::Source.new(@gem_repo)
def src.fetch_spec(name)
raise Gem::RemoteFetcher::FetchError.build("bad news from the internet", @uri)
raise Gem::RemoteFetcher::FetchError.new("bad news from the internet", @uri)
end

Gem.sources.replace [src]
Expand Down
6 changes: 3 additions & 3 deletions test/rubygems/test_remote_fetch_error.rb
Expand Up @@ -3,17 +3,17 @@

class TestRemoteFetchError < Gem::TestCase
def test_password_redacted
error = Gem::RemoteFetcher::FetchError.build('There was an error fetching', 'https://user:secret@gemsource.org')
error = Gem::RemoteFetcher::FetchError.new('There was an error fetching', 'https://user:secret@gemsource.org')
refute_match %r{secret}, error.to_s
end

def test_invalid_url
error = Gem::RemoteFetcher::FetchError.build('There was an error fetching', 'https://::gemsource.org')
error = Gem::RemoteFetcher::FetchError.new('There was an error fetching', 'https://::gemsource.org')
assert_equal error.to_s, 'There was an error fetching (https://::gemsource.org)'
end

def test_to_s
error = Gem::RemoteFetcher::FetchError.build('There was an error fetching', 'https://gemsource.org')
error = Gem::RemoteFetcher::FetchError.new('There was an error fetching', 'https://gemsource.org')
assert_equal error.to_s, 'There was an error fetching (https://gemsource.org)'
end
end
4 changes: 2 additions & 2 deletions test/rubygems/utilities.rb
Expand Up @@ -51,7 +51,7 @@ def find_data(path)
raise ArgumentError, 'need full URI' unless path.start_with?("https://", "http://")

unless @data.key? path
raise Gem::RemoteFetcher::FetchError.build("no data for #{path}", path)
raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
end

if @data[path].kind_of?(Array) && @data[path].first.kind_of?(Array)
Expand Down Expand Up @@ -124,7 +124,7 @@ def fetch_size(path)
raise ArgumentError, 'need full URI' unless path =~ %r{^http://}

unless @data.key? path
raise Gem::RemoteFetcher::FetchError.build("no data for #{path}", path)
raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
end

data = @data[path]
Expand Down

0 comments on commit 21dcdd2

Please sign in to comment.