Skip to content

Commit 31ba47c

Browse files
committed
! A proper fix for the error I've found yesterday.
We should further look for empty rescue statements, as they may sometimes catch stuff that we don't want to be caught.
1 parent 6a17e6a commit 31ba47c

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

lib/net/ldap.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'openssl'
22
require 'ostruct'
3-
43
require 'socket'
54

65
require 'net/ber'
@@ -1122,8 +1121,10 @@ class Connection # :nodoc:
11221121
def initialize server
11231122
begin
11241123
@conn = TCPSocket.new( server[:host], server[:port] )
1125-
rescue
1126-
raise LdapError.new( "no connection to server" )
1124+
rescue SocketError
1125+
raise LdapError, "No such address or other socket error."
1126+
rescue Errno::ECONNREFUSED
1127+
raise LdapError, "Server #{server[:host]} refused connection on port #{server[:port]}."
11271128
end
11281129

11291130
if server[:encryption]

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'net/ldap'
2+
13
Spec::Runner.configure do |config|
24
config.mock_with :flexmock
35
end

spec/unit/ldap_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'spec_helper'
2+
3+
describe Net::LDAP::Connection do
4+
describe "initialize" do
5+
context "when host is not responding" do
6+
before(:each) do
7+
flexmock(TCPSocket).
8+
should_receive(:new).and_raise(Errno::ECONNREFUSED)
9+
end
10+
11+
it "should raise LdapError" do
12+
lambda {
13+
Net::LDAP::Connection.new(
14+
:server => 'test.mocked.com',
15+
:port => 636)
16+
}.should raise_error(Net::LDAP::LdapError)
17+
end
18+
end
19+
context "when host is blocking the port" do
20+
before(:each) do
21+
flexmock(TCPSocket).
22+
should_receive(:new).and_raise(SocketError)
23+
end
24+
25+
it "should raise LdapError" do
26+
lambda {
27+
Net::LDAP::Connection.new(
28+
:server => 'test.mocked.com',
29+
:port => 636)
30+
}.should raise_error(Net::LDAP::LdapError)
31+
end
32+
end
33+
context "on other exceptions" do
34+
before(:each) do
35+
flexmock(TCPSocket).
36+
should_receive(:new).and_raise(NameError)
37+
end
38+
39+
it "should rethrow the exception" do
40+
lambda {
41+
Net::LDAP::Connection.new(
42+
:server => 'test.mocked.com',
43+
:port => 636)
44+
}.should raise_error(NameError)
45+
end
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)