Skip to content

Commit

Permalink
Less wrapping of network errors
Browse files Browse the repository at this point in the history
I don't think it's useful to wrap network errors like this, but I might
be missing something. I believe they tend to hide errors from the users.
If something in particular should be better worded, I believe the
builtin exception should be changed instead.
  • Loading branch information
deivid-rodriguez committed Mar 2, 2021
1 parent 8a712a0 commit 9ab6871
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/rubygems/remote_fetcher.rb
Expand Up @@ -51,6 +51,7 @@ def to_s # :nodoc:

class UnknownHostError < FetchError
end
deprecate_constant(:UnknownHostError)

@fetcher = nil

Expand Down Expand Up @@ -262,15 +263,9 @@ def fetch_path(uri, mtime = nil, head = false)
end

data
rescue Timeout::Error
raise UnknownHostError.new('timed out', uri)
rescue IOError, SocketError, SystemCallError,
rescue Timeout::Error, IOError, SocketError, SystemCallError,
*(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e
if e.message =~ /getaddrinfo/
raise UnknownHostError.new('no such name', uri)
else
raise FetchError.new("#{e.class}: #{e}", uri)
end
raise FetchError.new("#{e.class}: #{e}", uri)
end

def fetch_s3(uri, mtime = nil, head = false)
Expand Down
38 changes: 38 additions & 0 deletions test/rubygems/test_gem_remote_fetcher.rb
Expand Up @@ -496,6 +496,44 @@ def fetcher.fetch_http(uri, mtime = nil, head = nil)
assert_equal url, e.uri
end

def test_fetch_path_timeout_error
fetcher = Gem::RemoteFetcher.new nil
@fetcher = fetcher

def fetcher.fetch_http(uri, mtime = nil, head = nil)
raise Timeout::Error, 'timed out'
end

url = 'http://example.com/uri'

e = assert_raises Gem::RemoteFetcher::FetchError do
fetcher.fetch_path url
end

assert_match %r{Timeout::Error: timed out \(#{Regexp.escape url}\)\z},
e.message
assert_equal url, e.uri
end

def test_fetch_path_getaddrinfo_error
fetcher = Gem::RemoteFetcher.new nil
@fetcher = fetcher

def fetcher.fetch_http(uri, mtime = nil, head = nil)
raise SocketError, 'getaddrinfo: nodename nor servname provided'
end

url = 'http://example.com/uri'

e = assert_raises Gem::RemoteFetcher::FetchError do
fetcher.fetch_path url
end

assert_match %r{SocketError: getaddrinfo: nodename nor servname provided \(#{Regexp.escape url}\)\z},
e.message
assert_equal url, e.uri
end

def test_fetch_path_openssl_ssl_sslerror
fetcher = Gem::RemoteFetcher.new nil
@fetcher = fetcher
Expand Down

0 comments on commit 9ab6871

Please sign in to comment.