Search code examples
javaquarkusmutiny

Quarkus Mutiny Multi Exception handling if no result


I need to throw a specific exception in the case if the multi stream doesn't return anything (empty / null in terms of multi stream result);

Here is my repository method:

return reactiveRepository.findAllById(idList);

which returns a

Multi<WhateverModel>

Is there a way for me to throw an exception if the above repository method doesn't return anything / null / empty stream?

I've tried filter, but it doesn't get invoked if the underlying repo method doesn't return a result.


Solution

  • You can check if the streams was empty on completion:

    stream.onCompletion().ifEmpty().continueWith("a", "b", "c");
    

    That's probably what you need in your case. You can also check that you have received items before a timeout:

    stream.ifNoItem().after(timeout)
        .recoverWithMulti(Multi.createFrom().items("a", "b", "c"))
    

    However, not that Multi may not be what you are looking for here. It is often better to use Uni<List<?>> when dealing with a relational database, as their protocol does not support streaming, and, basically, it requires emulating streams in the application by keeping a connection opened.