Ldap Exceptions

Ldap Library uses exceptions to report most of errors. Exceptions used by The Ldap Library are defined in Korowai\Lib\Ldap\Exception namespace. The following exception classes are currently defined:

Ldap library’s exceptions
Exception Base Exception Thrown when
AttributeException OutOfRangeException accessing nonexistent attribute of an LDAP Entry
LdapException ErrorException an error occurs during an LDAP operation

AttributeException

Derived from OutOfRangeException. It’s being thrown when accessing nonexistent attribute of an LDAP Entry. For example

1
        $entry->getAttribute('inexistent');

LdapException

Derived from ErrorException. It’s being thrown when an LDAP operation fails. The exception message and code are taken from the LDAP backend.

1
2
3
4
5
6
try {
    $ldap->search('dc=inexistent,dc=org', 'cn=admin');
} catch (LdapException $e) {
    fprintf(STDERR, "LdapException(0x%x): %s\n", $e->getCode(), $e->getMessage());
    exit(1);
}

The output from above example is the following

1
LdapException(0x20): No such object

To handle particular LDAP errors in an application, exception code may be used

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
try {
    $ldap->search('dc=inexistent,dc=org', 'cn=admin');
} catch (LdapException $e) {
    if ($e->getCode() == 0x20) { /* No such object */
        fprintf(STDERR, "Warning(0x%x): %s\n", $e->getCode(), $e->getMessage());
        exit(2);
    } else {
        fprintf(STDERR, "LdapException(0x%x): %s\n", $e->getCode(), $e->getMessage());
        exit(1);
    }
}

The output from above example is the following

1
Warning(0x20): No such object

Standard LDAP result codes (including error codes) are defined in several documents including RFC 4511, RFC 3928, RFC 3909, RFC 4528, and RFC 4370. An authoritative source of LDAP result codes is the IANA registry. A useful list of LDAP return codes may also be found on LDAP Wiki.