Handling checked exception in Mono flow

2.1k views Asked by At

Not sure how to handle checked exception in the Mono flow.

return Mono.when(monoPubs)
               .zipWhen((monos) -> repository.findById(...))
               .map((tuple) -> tuple.getT2())
               .zipWhen((org) -> createMap(org))
               .map((tuple) -> tuple.getT2())
               .zipWhen((map) -> emailService.sendEmail(...))
               .flatMap(response -> {
                   return Mono.just(userId);
               });

Here, the sendEmail method is declared with throws Exception.

public Mono<Boolean> sendEmail(...)
            throws MessagingException, IOException

So, How to handle this checked exception in the zipWhen flow.

Also, How to handle

.zipWhen((map) -> emailService.sendEmail(...))

if the method returns void.

1

There are 1 answers

0
Alex On

You need to review implementation of the sendEmail. You cannot throw checked exceptions from the publisher and need to wrap any checked exception into an unchecked exception.

The Exceptions class provides a propagate method that could be used to wrap any checked exception into an unchecked exception.

try {
    ...
}
catch (SomeCheckedException e) {
    throw Exceptions.propagate(e);
}

As an alternative, you could use lombok @SneakyThrows to wrap non-reactive method.