Skip to content

Commit

Permalink
Merge pull request lostisland#129 from nbibler/net_http_exception_wra…
Browse files Browse the repository at this point in the history
…pping

Wrap known exceptions which bubble up through Net::HTTP
  • Loading branch information
technoweenie committed Apr 14, 2012
2 parents 9f1181c + b470b42 commit d68d862
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
17 changes: 16 additions & 1 deletion lib/faraday/adapter/net_http.rb
Expand Up @@ -8,6 +8,21 @@
module Faraday
class Adapter
class NetHttp < Faraday::Adapter
NET_HTTP_EXCEPTIONS = [
EOFError,
Errno::ECONNABORTED,
Errno::ECONNREFUSED,
Errno::ECONNRESET,
Errno::EINVAL,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
SocketError
]

NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)


def call(env)
super
url = env[:url]
Expand Down Expand Up @@ -61,7 +76,7 @@ def call(env)
else
http.request http_request, env[:body]
end
rescue Errno::ECONNREFUSED
rescue *NET_HTTP_EXCEPTIONS
raise Error::ConnectionFailed, $!
end

Expand Down
31 changes: 28 additions & 3 deletions test/adapters/net_http_test.rb
Expand Up @@ -22,10 +22,35 @@ def test_handles_compression_transparently_on_get
@connection.get('/hello')
end

def test_connect_error_gets_wrapped
stub_request(:get, 'disney.com/hello').to_raise(Errno::ECONNREFUSED)
def test_connection_errors_get_wrapped
exceptions = [
EOFError,
Errno::ECONNABORTED,
Errno::ECONNREFUSED,
Errno::ECONNRESET,
Errno::EINVAL,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
SocketError
]

assert_raise Faraday::Error::ConnectionFailed do
exceptions << OpenSSL::SSL::SSLError if defined?(OpenSSL)

exceptions.each do |exception_class|
stub_request(:get, 'disney.com/hello').to_raise(exception_class)

assert_raise(Faraday::Error::ConnectionFailed,
"Failed to wrap #{exception_class} exceptions") do
@connection.get('/hello')
end
end
end

def test_timeout_errors_get_wrapped
stub_request(:get, 'disney.com/hello').to_raise(Timeout::Error)

assert_raise Faraday::Error::TimeoutError do
@connection.get('/hello')
end
end
Expand Down

0 comments on commit d68d862

Please sign in to comment.