Browse project docs

Error API

Handle ldapjs-community client failures and return protocol-correct LDAP errors from server routes.

On this page

Protocol errors extend LDAPError and expose code, name, message, dn, and the standard JavaScript stack. The package exports LDAP status constants as well as corresponding error constructors.

Link to Handle client errorsHandle client errors

Operation callbacks receive an error when the server returns an unexpected LDAP result or the operation fails locally:

javascript
client.bind(process.env.LDAP_BIND_DN, process.env.LDAP_PASSWORD, (err) => {
  if (err instanceof ldap.InvalidCredentialsError) {
    console.error('Credentials were rejected')
    client.destroy(err)
    return
  }

  if (err) {
    console.error(err.name, err.code, err.message)
    client.destroy(err)
    return
  }

  client.unbind()
})

The client also defines ConnectionError, AbandonedError, and TimeoutError for failures that are not LDAP result codes. Search result status still arrives on the response emitter's end event; see Client API search.

Link to Return errors from a serverReturn errors from a server

Pass an LDAP error to next() to stop the remaining handlers and return the corresponding LDAP result:

javascript
server.search('dc=example,dc=com', (req, res, next) => {
  if (!req.connection.ldap.bindDN.equals(process.env.LDAP_BIND_DN)) {
    return next(new ldap.InsufficientAccessRightsError())
  }

  return next()
})

Common constructors include:

ErrorTypical meaning
InvalidCredentialsErrorBind credentials were rejected.
InsufficientAccessRightsErrorThe bound identity cannot perform the operation.
NoSuchObjectErrorThe target DN does not exist.
NoSuchAttributeErrorA requested attribute does not exist.
EntryAlreadyExistsErrorAn add conflicts with an existing DN.
ConstraintViolationErrorThe request violates a directory constraint.
InvalidDnSyntaxErrorA DN is not valid for the operation.
InvalidAttributeSyntaxErrorAn attribute value does not satisfy its syntax.
ProtocolErrorThe request violates LDAP protocol requirements.
OperationsErrorThe server cannot complete the operation.

This table is intentionally not exhaustive. Constructors are generated from the status constants exported by the current package, including cancel and control-related result codes.

Link to Complete referenceComplete reference