Skip to content

How to deal with dropped exceptions?#

There are a few corner cases where Mutiny cannot propagate an exception to a Uni or a Multi subscriber.

Consider the following example:

1
2
3
4
5
6
Cancellable cancellable = Uni.createFrom()
        .emitter(this::emitter)
        .onCancellation().call(() -> Uni.createFrom().failure(new IOException("boom")))
        .subscribe().with(this::onItem, this::onFailure);

cancellable.cancel();

The onCancellation().call(...) method is called when the Uni subscription is cancelled. The returned Uni is failed with a IOException, but since the subscription itself has been cancelled then there is no way to catch the exception.

By default Mutiny reports such dropped exceptions to the standard error stream along with the corresponding stack trace. You can change how these exceptions are handled using Infrastructure.setDroppedExceptionHandler.

The following logs dropped exceptions to a logger:

1
2
3
Infrastructure.setDroppedExceptionHandler(err ->
        log(Level.SEVERE, "Mutiny dropped exception", err)
);