I must connect to a LDAP server and find user based on a specific ID.
I've decided to use ldapjs module.
I managed to create my client and bind him so I can search for an user with success.
My problem is, as I only use async/await, that I don't understant how to handle error in callbacks... For example with this simple code from ldapjs library :
public static async search(searchOptions: SearchOptions) {
LdapService.bind()
LdapService.getClient()?.search('ou=*****,ou=***,dc=****,dc=****', searchOptions, function (err, res) {
ifError(err)
res.on('searchEntry', function (entry) {
// ----------------------
// an error came from here
throw new Error('test')
// ----------------------
console.log('entry: ' + JSON.stringify(entry.object));
});
res.on('searchReference', function (referral) {
console.log('referral: ' + referral.uris.join());
});
res.on('error', function (err) {
console.error('error: ' + err.message);
});
res.on('end', function (result) {
console.log('status: ' + result?.status);
});
})
}
LdapService.getClient() is a singleton method that return the result of createClient -> works fine
LdapService.bind() is a method that just bind with the server with correct credentials -> works fine
I just can't manage to handle my error "test"... How am I supposed to handle it?
Is the search method really async?
Can I do it the async/await way? :P
PS: the DN string ("ou=,ou=,dc=,dc=") is hidden due to security reasons and the code works great without throwing an error ;)