Source code for ldaptor.protocols.ldap.ldaperrors
from ldaptor._encoder import to_bytes
[docs]def get(resultCode, errorMessage):
"""Get an instance of the correct exception for this resultCode."""
return LDAPExceptionCollection.get_instance(resultCode, errorMessage)
[docs]class LDAPExceptionCollection(type):
"""
Storage for the LDAP result codes and
the corresponding classes.
"""
collection = {}
def __new__(mcs, name, bases, attributes):
cls = type.__new__(mcs, name, bases, attributes)
code = attributes.get("resultCode")
if code is not None:
assert isinstance(code, int)
assert isinstance(attributes.get("name"), bytes)
mcs.collection[code] = cls
return cls
[docs] @classmethod
def get_instance(mcs, code, message):
"""Get an instance of the correct exception for this result code."""
cls = mcs.collection.get(code)
if cls is not None:
return cls(message)
return LDAPUnknownError(code, message)
[docs]class LDAPResult(metaclass=LDAPExceptionCollection):
resultCode = None
name = None
[docs]class Success(LDAPResult):
resultCode = 0
name = b"success"
def __init__(self, msg):
pass
[docs]class LDAPException(Exception, LDAPResult):
def __init__(self, message=None):
Exception.__init__(self)
self.message = message
def __str__(self):
message = self.toWire()
return message.decode("utf-8")
[docs] def toWire(self):
if self.message:
return b"%s: %s" % (self.name, to_bytes(self.message))
if self.name:
return self.name
return b"Unknown LDAP error %r" % self
[docs]class LDAPUnknownError(LDAPException):
def __init__(self, resultCode, message=None):
assert resultCode not in LDAPExceptionCollection.collection, (
"resultCode %r must be unknown" % resultCode
)
self.code = resultCode
LDAPException.__init__(self, message)
[docs] def toWire(self):
codeName = b"unknownError(%d)" % self.code
if self.message:
return b"%s: %s" % (codeName, to_bytes(self.message))
else:
return codeName
[docs]class LDAPOperationsError(LDAPException):
resultCode = 1
name = b"operationsError"
[docs]class LDAPProtocolError(LDAPException):
resultCode = 2
name = b"protocolError"
[docs]class LDAPTimeLimitExceeded(LDAPException):
resultCode = 3
name = b"timeLimitExceeded"
[docs]class LDAPSizeLimitExceeded(LDAPException):
resultCode = 4
name = b"sizeLimitExceeded"
[docs]class LDAPCompareFalse(LDAPException):
resultCode = 5
name = b"compareFalse"
[docs]class LDAPCompareTrue(LDAPException):
resultCode = 6
name = b"compareTrue"
[docs]class LDAPAuthMethodNotSupported(LDAPException):
resultCode = 7
name = b"authMethodNotSupported"
[docs]class LDAPStrongAuthRequired(LDAPException):
resultCode = 8
name = b"strongAuthRequired"
# 9 reserved
[docs]class LDAPReferral(LDAPException):
resultCode = 10
name = b"referral"
[docs]class LDAPAdminLimitExceeded(LDAPException):
resultCode = 11
name = b"adminLimitExceeded"
[docs]class LDAPUnavailableCriticalExtension(LDAPException):
resultCode = 12
name = b"unavailableCriticalExtension"
[docs]class LDAPConfidentialityRequired(LDAPException):
resultCode = 13
name = b"confidentialityRequired"
[docs]class LDAPSaslBindInProgress(LDAPException):
resultCode = 14
name = b"saslBindInProgress"
[docs]class LDAPNoSuchAttribute(LDAPException):
resultCode = 16
name = b"noSuchAttribute"
[docs]class LDAPUndefinedAttributeType(LDAPException):
resultCode = 17
name = b"undefinedAttributeType"
[docs]class LDAPInappropriateMatching(LDAPException):
resultCode = 18
name = b"inappropriateMatching"
[docs]class LDAPConstraintViolation(LDAPException):
resultCode = 19
name = b"constraintViolation"
[docs]class LDAPAttributeOrValueExists(LDAPException):
resultCode = 20
name = b"attributeOrValueExists"
[docs]class LDAPInvalidAttributeSyntax(LDAPException):
resultCode = 21
name = b"invalidAttributeSyntax"
# 22-31 unused
[docs]class LDAPNoSuchObject(LDAPException):
resultCode = 32
name = b"noSuchObject"
[docs]class LDAPAliasProblem(LDAPException):
resultCode = 33
name = b"aliasProblem"
[docs]class LDAPInvalidDNSyntax(LDAPException):
resultCode = 34
name = b"invalidDNSyntax"
# 35 reserved for undefined isLeaf
[docs]class LDAPAliasDereferencingProblem(LDAPException):
resultCode = 36
name = b"aliasDereferencingProblem"
# 37-47 unused
[docs]class LDAPInappropriateAuthentication(LDAPException):
resultCode = 48
name = b"inappropriateAuthentication"
[docs]class LDAPInvalidCredentials(LDAPException):
resultCode = 49
name = b"invalidCredentials"
[docs]class LDAPInsufficientAccessRights(LDAPException):
resultCode = 50
name = b"insufficientAccessRights"
[docs]class LDAPBusy(LDAPException):
resultCode = 51
name = b"busy"
[docs]class LDAPUnavailable(LDAPException):
resultCode = 52
name = b"unavailable"
[docs]class LDAPLoopDetect(LDAPException):
resultCode = 54
name = b"loopDetect"
# 55-63 unused
[docs]class LDAPNamingViolation(LDAPException):
resultCode = 64
name = b"namingViolation"
[docs]class LDAPObjectClassViolation(LDAPException):
resultCode = 65
name = b"objectClassViolation"
[docs]class LDAPNotAllowedOnNonLeaf(LDAPException):
resultCode = 66
name = b"notAllowedOnNonLeaf"
[docs]class LDAPNotAllowedOnRDN(LDAPException):
resultCode = 67
name = b"notAllowedOnRDN"
[docs]class LDAPEntryAlreadyExists(LDAPException):
resultCode = 68
name = b"entryAlreadyExists"
[docs]class LDAPObjectClassModsProhibited(LDAPException):
resultCode = 69
name = b"objectClassModsProhibited"
# 70 reserved for CLDAP
[docs]class LDAPAffectsMultipleDSAs(LDAPException):
resultCode = 71
name = b"affectsMultipleDSAs"
# 72-79 unused
[docs]class LDAPOther(LDAPException):
resultCode = 80
name = b"other"
# 81-90 reserved for APIs
# Backwards compatibility
other = LDAPOther.resultCode
reverse = LDAPExceptionCollection.collection