1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[rubygems/rubygems] Refactor Gem::RemoteFetcher::FetchError.build back to its initialize method

https://github.com/rubygems/rubygems/commit/21dcdd2dc5
This commit is contained in:
Daniel Niknam 2021-08-22 20:06:02 +10:00 committed by Hiroshi SHIBATA
parent a508693f06
commit 589377fbdc
Notes: git 2021-08-31 19:06:56 +09:00
8 changed files with 28 additions and 37 deletions

View file

@ -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)
@uri = uri
@original_uri = original_uri ? original_uri : uri
super(uri.valid_uri? && uri.original_password ? message.sub(uri.original_password, 'REDACTED') : message)
@uri = uri.to_s
end
def to_s # :nodoc:
@ -225,20 +216,20 @@ class Gem::RemoteFetcher
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
@ -260,21 +251,21 @@ class Gem::RemoteFetcher
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

View file

@ -127,7 +127,7 @@ class Gem::Request
@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
@ -228,14 +228,14 @@ class Gem::Request
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.
@ -245,7 +245,7 @@ class Gem::Request
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

View file

@ -182,7 +182,7 @@ class TestGemCommandsSourcesCommand < Gem::TestCase
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]

View file

@ -204,7 +204,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg==
@test_data
end
raise Gem::RemoteFetcher::FetchError.build("haha!", '')
raise Gem::RemoteFetcher::FetchError.new("haha!", '')
end
end

View file

@ -106,7 +106,7 @@ class TestGemResolverBestSet < Gem::TestCase
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
@ -124,7 +124,7 @@ class TestGemResolverBestSet < Gem::TestCase
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
@ -145,7 +145,7 @@ class TestGemResolverBestSet < Gem::TestCase
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

View file

@ -144,7 +144,7 @@ class TestGemSpecFetcher < Gem::TestCase
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]

View file

@ -3,17 +3,17 @@ require_relative 'helper'
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

View file

@ -51,7 +51,7 @@ class Gem::FakeFetcher
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)
@ -124,7 +124,7 @@ class Gem::FakeFetcher
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]