I have written a function, in Kotlin, where I've used the non null assertion in the method signature as follows:
saveEntity(param1!!, param2!!)
.onErrorResume {
logger.error("Exception caught", it)
Mono.just(someObject)
}
Just for reference, the saveEntity method returns a Mono<someObject>. The issue is, when param1 or param2 is null and the code throws a NullPointerException, it's not a Mono and I'm not able to handle it in onErrorResume. Is there a way to throw a Mono.error() in the function call itself using any ternary type operations so that this exception can be caught in onErrorResume()?
As you are using Kotlin, you could write a lambda utility for this, along the lines of:
Note that I have not checked whether this compiles, but tweaked, it should do what you need:
The reason this works is that the null check is done inside the lambda expression and thus is inside the inner try-catch block. Be aware that this requires
param1andparam2to be (at least) effectively final.