Skip to content

Commit

Permalink
Move connection error handling logic into a new error class
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Jeremy Bopp committed Oct 18, 2015
1 parent d5f7516 commit edee2ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
9 changes: 1 addition & 8 deletions lib/net/ldap/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ def open_connection(server)
end
end

if errors.size == 1
error = errors.first.first
raise Net::LDAP::ConnectionRefusedError, error.message if error.kind_of? Errno::ECONNREFUSED
raise Net::LDAP::Error, error.message
end

raise Net::LDAP::Error,
"Unable to connect to any given server: \n #{errors.map { |e, h, p| "#{e.class}: #{e.message} (#{h}:#{p})" }.join("\n ")}"
raise Net::LDAP::ConnectionError.new(errors)
end

module GetbyteForSSLSocket
Expand Down
19 changes: 19 additions & 0 deletions lib/net/ldap/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ def warn_deprecation_message
warn "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead."
end
end
class ConnectionError < Error
def self.new(errors)
error = errors.first.first
if errors.size == 1
if error.kind_of? Errno::ECONNREFUSED
return Net::LDAP::ConnectionRefusedError.new(error.message)
end

return Net::LDAP::Error.new(error.message)
end

super
end

def initialize(errors)
message = "Unable to connect to any given server: \n #{errors.map { |e, h, p| "#{e.class}: #{e.message} (#{h}:#{p})" }.join("\n ")}"
super(message)
end
end
class NoOpenSSLError < Error; end
class NoStartTLSResultError < Error; end
class NoSearchBaseError < Error; end
Expand Down
2 changes: 1 addition & 1 deletion test/test_ldap_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_list_of_hosts_with_all_hosts_failure
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[1]).once.and_raise(SocketError)
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[2]).once.and_raise(SocketError)
flexmock(TCPSocket).should_receive(:new).ordered.never
assert_raise Net::LDAP::Error do
assert_raise Net::LDAP::ConnectionError do
Net::LDAP::Connection.new(:hosts => hosts)
end
end
Expand Down

0 comments on commit edee2ee

Please sign in to comment.