Skip to content

Commit 120d8c8

Browse files
author
Tatsuya Sato
committed
Replace Net::LDAP::Error with proper subsclasses
1 parent c9d36cd commit 120d8c8

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

lib/net/ldap/connection.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def close
4242
end
4343

4444
def self.wrap_with_ssl(io, tls_options = {})
45-
raise Net::LDAP::Error, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL
45+
raise Net::LDAP::NoOpenSSLError, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL
4646

4747
ctx = OpenSSL::SSL::SSLContext.new
4848

@@ -105,16 +105,16 @@ def setup_encryption(args)
105105
pdu = queued_read(message_id)
106106

107107
if pdu.nil? || pdu.app_tag != Net::LDAP::PDU::ExtendedResponse
108-
raise Net::LDAP::Error, "no start_tls result"
108+
raise Net::LDAP::NoStartTLSResultError, "no start_tls result"
109109
end
110110

111111
if pdu.result_code.zero?
112112
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options])
113113
else
114-
raise Net::LDAP::Error, "start_tls failed: #{pdu.result_code}"
114+
raise Net::LDAP::StartTlSError, "start_tls failed: #{pdu.result_code}"
115115
end
116116
else
117-
raise Net::LDAP::Error, "unsupported encryption method #{args[:method]}"
117+
raise Net::LDAP::EncMethodUnsupportedError, "unsupported encryption method #{args[:method]}"
118118
end
119119
end
120120

@@ -225,7 +225,7 @@ def bind(auth)
225225
elsif meth == :gss_spnego
226226
bind_gss_spnego(auth)
227227
else
228-
raise Net::LDAP::Error, "Unsupported auth method (#{meth})"
228+
raise Net::LDAP::AuthMethodUnsupportedError, "Unsupported auth method (#{meth})"
229229
end
230230
end
231231
end
@@ -241,7 +241,7 @@ def bind_simple(auth)
241241
["", ""]
242242
end
243243

244-
raise Net::LDAP::Error, "Invalid binding information" unless (user && psw)
244+
raise Net::LDAP::BindingInformationInvalidError, "Invalid binding information" unless (user && psw)
245245

246246
message_id = next_msgid
247247
request = [
@@ -253,7 +253,7 @@ def bind_simple(auth)
253253
pdu = queued_read(message_id)
254254

255255
if !pdu || pdu.app_tag != Net::LDAP::PDU::BindResult
256-
raise Net::LDAP::Error, "no bind result"
256+
raise Net::LDAP::NoBindResultError, "no bind result"
257257
end
258258

259259
pdu
@@ -283,7 +283,7 @@ def bind_simple(auth)
283283
def bind_sasl(auth)
284284
mech, cred, chall = auth[:mechanism], auth[:initial_credential],
285285
auth[:challenge_response]
286-
raise Net::LDAP::Error, "Invalid binding information" unless (mech && cred && chall)
286+
raise Net::LDAP::BindingInformationInvalidError, "Invalid binding information" unless (mech && cred && chall)
287287

288288
message_id = next_msgid
289289

@@ -298,16 +298,16 @@ def bind_sasl(auth)
298298
pdu = queued_read(message_id)
299299

300300
if !pdu || pdu.app_tag != Net::LDAP::PDU::BindResult
301-
raise Net::LDAP::Error, "no bind result"
301+
raise Net::LDAP::NoBindResultError, "no bind result"
302302
end
303303

304304
return pdu unless pdu.result_code == Net::LDAP::ResultCodeSaslBindInProgress
305-
raise Net::LDAP::Error, "sasl-challenge overflow" if ((n += 1) > MaxSaslChallenges)
305+
raise Net::LDAP::SASLChallengeOverflowError, "sasl-challenge overflow" if ((n += 1) > MaxSaslChallenges)
306306

307307
cred = chall.call(pdu.result_server_sasl_creds)
308308
}
309309

310-
raise Net::LDAP::Error, "why are we here?"
310+
raise Net::LDAP::SASLChallengeOverflowError, "why are we here?"
311311
end
312312
private :bind_sasl
313313

@@ -326,7 +326,7 @@ def bind_gss_spnego(auth)
326326
require 'ntlm'
327327

328328
user, psw = [auth[:username] || auth[:dn], auth[:password]]
329-
raise Net::LDAP::Error, "Invalid binding information" unless (user && psw)
329+
raise Net::LDAP::BindingInformationInvalidError, "Invalid binding information" unless (user && psw)
330330

331331
nego = proc { |challenge|
332332
t2_msg = NTLM::Message.parse(challenge)
@@ -412,9 +412,9 @@ def search(args = nil)
412412
sort = args.fetch(:sort_controls, false)
413413

414414
# arg validation
415-
raise Net::LDAP::Error, "search base is required" unless base
416-
raise Net::LDAP::Error, "invalid search-size" unless size >= 0
417-
raise Net::LDAP::Error, "invalid search scope" unless Net::LDAP::SearchScopes.include?(scope)
415+
raise Net::LDAP::NoSearchBaseError, "search base is required" unless base
416+
raise Net::LDAP::SearchSizeInvalidError, "invalid search-size" unless size >= 0
417+
raise Net::LDAP::SearchScopeInvalidError, "invalid search scope" unless Net::LDAP::SearchScopes.include?(scope)
418418
raise Net::LDAP::Error, "invalid alias dereferencing value" unless Net::LDAP::DerefAliasesArray.include?(deref)
419419

420420
# arg transforms
@@ -527,7 +527,7 @@ def search(args = nil)
527527
end
528528
break
529529
else
530-
raise Net::LDAP::Error, "invalid response-type in search: #{pdu.app_tag}"
530+
raise Net::LDAP::ResponseTypeInvalidError, "invalid response-type in search: #{pdu.app_tag}"
531531
end
532532
end
533533

@@ -624,7 +624,7 @@ def modify(args)
624624
pdu = queued_read(message_id)
625625

626626
if !pdu || pdu.app_tag != Net::LDAP::PDU::ModifyResponse
627-
raise Net::LDAP::Error, "response missing or invalid"
627+
raise Net::LDAP::ResponseMissingOrInvalidError, "response missing or invalid"
628628
end
629629

630630
pdu
@@ -638,7 +638,7 @@ def modify(args)
638638
# to the error message and the matched-DN returned by the server.
639639
#++
640640
def add(args)
641-
add_dn = args[:dn] or raise Net::LDAP::Error, "Unable to add empty DN"
641+
add_dn = args[:dn] or raise Net::LDAP::EmptyDNError, "Unable to add empty DN"
642642
add_attrs = []
643643
a = args[:attributes] and a.each { |k, v|
644644
add_attrs << [ k.to_s.to_ber, Array(v).map { |m| m.to_ber}.to_ber_set ].to_ber_sequence
@@ -651,7 +651,7 @@ def add(args)
651651
pdu = queued_read(message_id)
652652

653653
if !pdu || pdu.app_tag != Net::LDAP::PDU::AddResponse
654-
raise Net::LDAP::Error, "response missing or invalid"
654+
raise Net::LDAP::ResponseMissingError, "response missing or invalid"
655655
end
656656

657657
pdu
@@ -674,7 +674,7 @@ def rename(args)
674674
pdu = queued_read(message_id)
675675

676676
if !pdu || pdu.app_tag != Net::LDAP::PDU::ModifyRDNResponse
677-
raise Net::LDAP::Error.new "response missing or invalid"
677+
raise Net::LDAP::ResponseMissingOrInvalidError.new "response missing or invalid"
678678
end
679679

680680
pdu
@@ -693,7 +693,7 @@ def delete(args)
693693
pdu = queued_read(message_id)
694694

695695
if !pdu || pdu.app_tag != Net::LDAP::PDU::DeleteResponse
696-
raise Net::LDAP::Error, "response missing or invalid"
696+
raise Net::LDAP::ResponseMissingOrInvalidError, "response missing or invalid"
697697
end
698698

699699
pdu

lib/net/ldap/error.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SocketError < Error; end
1212
class ConnectionRefusedError < Error; end
1313
class NoOpenSSLError < Error; end
1414
class NoStartTLSResultError < Error; end
15+
class NoSearchBaseError < Error; end
1516
class StartTLSError < Error; end
1617
class EncryptionUnsupportedError < Error; end
1718
class EncMethodUnsupportedError < Error; end

0 commit comments

Comments
 (0)