Skip to content

Commit edee2ee

Browse files
author
Jeremy Bopp
committed
Move connection error handling logic into a new error class
* ConnectionError wraps the creation of several error types for backward compatibility * For now, ConnectionError is only created when more than 1 error is given to the constructor * In the future, ConnectionError should be used even in the single error case
1 parent d5f7516 commit edee2ee

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

lib/net/ldap/connection.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ def open_connection(server)
4848
end
4949
end
5050

51-
if errors.size == 1
52-
error = errors.first.first
53-
raise Net::LDAP::ConnectionRefusedError, error.message if error.kind_of? Errno::ECONNREFUSED
54-
raise Net::LDAP::Error, error.message
55-
end
56-
57-
raise Net::LDAP::Error,
58-
"Unable to connect to any given server: \n #{errors.map { |e, h, p| "#{e.class}: #{e.message} (#{h}:#{p})" }.join("\n ")}"
51+
raise Net::LDAP::ConnectionError.new(errors)
5952
end
6053

6154
module GetbyteForSSLSocket

lib/net/ldap/error.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ def warn_deprecation_message
2525
warn "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead."
2626
end
2727
end
28+
class ConnectionError < Error
29+
def self.new(errors)
30+
error = errors.first.first
31+
if errors.size == 1
32+
if error.kind_of? Errno::ECONNREFUSED
33+
return Net::LDAP::ConnectionRefusedError.new(error.message)
34+
end
35+
36+
return Net::LDAP::Error.new(error.message)
37+
end
38+
39+
super
40+
end
41+
42+
def initialize(errors)
43+
message = "Unable to connect to any given server: \n #{errors.map { |e, h, p| "#{e.class}: #{e.message} (#{h}:#{p})" }.join("\n ")}"
44+
super(message)
45+
end
46+
end
2847
class NoOpenSSLError < Error; end
2948
class NoStartTLSResultError < Error; end
3049
class NoSearchBaseError < Error; end

test/test_ldap_connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_list_of_hosts_with_all_hosts_failure
4242
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[1]).once.and_raise(SocketError)
4343
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[2]).once.and_raise(SocketError)
4444
flexmock(TCPSocket).should_receive(:new).ordered.never
45-
assert_raise Net::LDAP::Error do
45+
assert_raise Net::LDAP::ConnectionError do
4646
Net::LDAP::Connection.new(:hosts => hosts)
4747
end
4848
end

0 commit comments

Comments
 (0)