diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index b51bcc10..8e0e8c18 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -14,7 +14,7 @@ def initialize(server) rescue SocketError raise Net::LDAP::Error, "No such address or other socket error." rescue Errno::ECONNREFUSED - raise Net::LDAP::Error, "Server #{server[:host]} refused connection on port #{server[:port]}." + raise Net::LDAP::ConnectionRefusedError, "Server #{server[:host]} refused connection on port #{server[:port]}." rescue Errno::EHOSTUNREACH => error raise Net::LDAP::Error, "Host #{server[:host]} was unreachable (#{error.message})" rescue Errno::ETIMEDOUT diff --git a/lib/net/ldap/error.rb b/lib/net/ldap/error.rb index c9a25f90..38b4a4a5 100644 --- a/lib/net/ldap/error.rb +++ b/lib/net/ldap/error.rb @@ -9,7 +9,22 @@ class Error < StandardError; end class AlreadyOpenedError < Error; end class SocketError < Error; end - class ConnectionRefusedError < Error; end + class ConnectionRefusedError < Error; + def initialize(*args) + warn_deprecation_message + super + end + + def message + warn_deprecation_message + super + end + + private + def warn_deprecation_message + warn "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead." + end + end class NoOpenSSLError < Error; end class NoStartTLSResultError < Error; end class NoSearchBaseError < Error; end diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index 96b542ac..4fa7b22d 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -1,6 +1,14 @@ require_relative 'test_helper' class TestLDAPConnection < Test::Unit::TestCase + def capture_stderr + stderr, $stderr = $stderr, StringIO.new + yield + $stderr.string + ensure + $stderr = stderr + end + def test_unresponsive_host assert_raise Net::LDAP::Error do Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636) @@ -14,6 +22,16 @@ def test_blocked_port end end + def test_connection_refused + flexmock(TCPSocket).should_receive(:new).and_raise(Errno::ECONNREFUSED) + stderr = capture_stderr do + assert_raise Net::LDAP::ConnectionRefusedError do + Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636) + end + end + assert_equal("Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead.\n", stderr) + end + def test_raises_unknown_exceptions error = Class.new(StandardError) flexmock(TCPSocket).should_receive(:new).and_raise(error)