vt-middleware / ldaptive

A simple, extensible Java API for interacting with LDAP servers
56 stars 26 forks source link

onException / ExceptionHandler not called #185

Closed bremersee closed 3 years ago

bremersee commented 3 years ago

I execute an operation and expect, that the exception handler is called, because the entry doesn't exist. But it isn't called.

Example:

    DeleteOperation.builder()
        .factory(getConnectionFactory())
        .throwIf(ResultPredicate.NOT_SUCCESS)
        .onResult(result -> System.out.println("onResult = " + result.getResultCode()))
        .onException(ldapException -> System.out.println("onException = " + ldapException.getResultCode()))
        .build()
        .send(DeleteRequest.builder()
            .dn("cn=notexists,dc=...")
            .build());
    System.out.println("Sleeping");
    Thread.sleep(3000L);
    System.out.println("Done");

The current output is:

Sleeping
onResult = NO_SUCH_OBJECT
Done

Expected output:

Sleeping
onException = NO_SUCH_OBJECT
Done
dfish3r commented 3 years ago

The throwIf functionality was designed to propagate exceptions out of the handle for those who prefer to use a try-catch paradigm.

try {
    DeleteOperation.builder()
        .factory(getConnectionFactory())
        .throwIf(ResultPredicate.NOT_SUCCESS)
        .onResult(result -> System.out.println("onResult = " + result.getResultCode()))
        .onException(ldapException -> System.out.println("onException = " + ldapException.getResultCode()))
        .build()
        .execute(DeleteRequest.builder()
            .dn("cn=notexists,dc=...")
            .build());
} catch (LdapException ldapException) {
    System.out.println("catchException = " + ldapException.getResultCode())
}

If you prefer your implementation in a lamda, onResult should give you what you want. Note that using throwIf requires a synchronous style of programming as you must wait in the try block until a result is received.

bremersee commented 3 years ago

Many thanks for the answer. That fits my observations. But also when using "execute", "onException" / the ExceptionHandler is not called. Is it even called?

BTW: The async approach is great! 👍

dfish3r commented 3 years ago

onException gets invoked when something unusual happens, never for normal server responses.