Skip to content

Commit 04fd933

Browse files
binarycodeioquatix
authored andcommitted
wrap connection and timeout exceptions
1 parent f45e611 commit 04fd933

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/async/http/faraday/adapter.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ module Faraday
2929
PERSISTENT = ::Faraday::Connection.instance_methods.include?(:close)
3030

3131
class Adapter < ::Faraday::Adapter
32+
CONNECTION_EXCEPTIONS = [
33+
Errno::EADDRNOTAVAIL,
34+
Errno::ECONNABORTED,
35+
Errno::ECONNREFUSED,
36+
Errno::ECONNRESET,
37+
Errno::EHOSTUNREACH,
38+
Errno::EINVAL,
39+
Errno::ENETUNREACH,
40+
Errno::EPIPE,
41+
IOError,
42+
SocketError
43+
].freeze
44+
3245
def initialize(*arguments, **options, &block)
3346
super
3447

@@ -48,6 +61,12 @@ def call(env)
4861
save_response(env, response.status, response.read, response.headers)
4962

5063
return @app.call(env)
64+
rescue Errno::ETIMEDOUT => e
65+
raise ::Faraday::TimeoutError, e
66+
rescue OpenSSL::SSL::SSLError => e
67+
raise ::Faraday::SSLError, e
68+
rescue *CONNECTION_EXCEPTIONS => e
69+
raise ::Faraday::ConnectionFailed, e
5170
ensure
5271
# Don't retain persistent connections unless they will eventually be closed:
5372
@internet.close unless @persistent

spec/async/http/faraday/adapter_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,8 @@ def get_response(url, path)
100100
end.get('/index')
101101
end
102102
end
103+
104+
it 'wraps underlying exceptions into Faraday analogs' do
105+
expect { get_response(endpoint.url, '/index') }.to raise_error(Faraday::ConnectionFailed)
106+
end
103107
end

0 commit comments

Comments
 (0)