mirror of
https://github.com/lostisland/faraday.git
synced 2025-12-08 00:02:54 -05:00
Merge pull request #129 from nbibler/net_http_exception_wrapping
Wrap known exceptions which bubble up through Net::HTTP
This commit is contained in:
commit
d68d862310
@ -8,6 +8,21 @@ end
|
|||||||
module Faraday
|
module Faraday
|
||||||
class Adapter
|
class Adapter
|
||||||
class NetHttp < Faraday::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)
|
def call(env)
|
||||||
super
|
super
|
||||||
url = env[:url]
|
url = env[:url]
|
||||||
@ -61,7 +76,7 @@ module Faraday
|
|||||||
else
|
else
|
||||||
http.request http_request, env[:body]
|
http.request http_request, env[:body]
|
||||||
end
|
end
|
||||||
rescue Errno::ECONNREFUSED
|
rescue *NET_HTTP_EXCEPTIONS
|
||||||
raise Error::ConnectionFailed, $!
|
raise Error::ConnectionFailed, $!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -22,10 +22,35 @@ module Adapters
|
|||||||
@connection.get('/hello')
|
@connection.get('/hello')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_connect_error_gets_wrapped
|
def test_connection_errors_get_wrapped
|
||||||
stub_request(:get, 'disney.com/hello').to_raise(Errno::ECONNREFUSED)
|
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')
|
@connection.get('/hello')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user