Class UniOnFailure<T>
- java.lang.Object
-
- io.smallrye.mutiny.groups.UniOnFailure<T>
-
- Type Parameters:
T- the type of item
public class UniOnFailure<T> extends java.lang.ObjectConfigures the failure handler.The upstream uni has sent us a failure, this class lets you decide what need to be done in this case. Typically, you can recover with a fallback item (
recoverWithItem(Object)), or with another Uni (recoverWithUni(Uni)). You can also retry (retry()). Maybe, you just want to look at the failure (invoke(Consumer)).You can configure the type of failure on which your handler is called using:
uni.onFailure(IOException.class).recoverWithItem("boom") uni.onFailure(IllegalStateException.class).recoverWithItem("kaboom") uni.onFailure(t -> accept(t)).recoverWithItem("another boom")
-
-
Constructor Summary
Constructors Constructor Description UniOnFailure(Uni<T> upstream, java.util.function.Predicate<? super java.lang.Throwable> predicate)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Uni<T>call(java.util.function.Function<java.lang.Throwable,Uni<?>> action)Uni<T>call(java.util.function.Supplier<Uni<?>> supplier)Uni<T>invoke(java.lang.Runnable callback)Uni<T>invoke(java.util.function.Consumer<java.lang.Throwable> callback)Uni<T>recoverWithItem(java.util.function.Function<? super java.lang.Throwable,? extends T> function)Recovers from the received failure (matching the predicate if set) by using a item generated by the given function.Uni<T>recoverWithItem(java.util.function.Supplier<T> supplier)Recovers from the received failure (matching the predicate if set) by using a item generated by the given supplier.Uni<T>recoverWithItem(T fallback)Recovers from the received failure (matching the predicate if set) by using a fallback item.Uni<T>recoverWithNull()Recovers from the received failure by ignoring it and emitting anullitem in the resultingUni.Uni<T>recoverWithUni(Uni<? extends T> fallback)Recovers from the received failure (matching the predicate if set) with anotherUni.Uni<T>recoverWithUni(java.util.function.Function<? super java.lang.Throwable,Uni<? extends T>> function)Recovers from the received failure (matching the predicate if set) with anotherUni.Uni<T>recoverWithUni(java.util.function.Supplier<Uni<? extends T>> supplier)Recovers from the received failure (matching the predicate if set) with anotherUni.UniRetry<T>retry()Configures the retry strategy.Uni<T>transform(java.util.function.Function<? super java.lang.Throwable,? extends java.lang.Throwable> mapper)
-
-
-
Method Detail
-
invoke
public Uni<T> invoke(java.util.function.Consumer<java.lang.Throwable> callback)
Produces a newUniinvoking the given callback when thisUniemits a failure (matching the predicate if set).If the callback throws an exception, a
CompositeExceptionis propagated downstream. This exception is composed by the received failure and the thrown exception.- Parameters:
callback- the callback, must not benull- Returns:
- the new
Uni
-
invoke
public Uni<T> invoke(java.lang.Runnable callback)
Produces a newUniinvoking the given callback when thisUniemits a failure (matching the predicate if set).If the callback throws an exception, a
CompositeExceptionis propagated downstream. This exception is composed by the received failure and the thrown exception.- Parameters:
callback- the callback, must not benull- Returns:
- the new
Uni
-
call
public Uni<T> call(java.util.function.Function<java.lang.Throwable,Uni<?>> action)
Produces a newUniinvoking the given function when the currentUnipropagates a failure (matching the predicate if set). The function can transform the received failure into another exception that will be fired as failure downstream. Produces a newUniinvoking the given @{code action} when thefailureevent is received.Unlike
invoke(Consumer), the passed function returns aUni. When the producedUnisends its item, this item is discarded, and the originalfailureis forwarded downstream. If the producedUnifails, a composite failure containing both the original failure and the failure from the executed action is propagated downstream.- Parameters:
action- the callback, must not benull- Returns:
- the new
Uni
-
call
public Uni<T> call(java.util.function.Supplier<Uni<?>> supplier)
Produces a newUniinvoking the given supplier when the currentUnipropagates a failure (matching the predicate if set). The supplier ignores the failure. Produces a newUniinvoking the given @{code supplier} when thefailureevent is received.Unlike
invoke(Consumer), the passed function returns aUni. When the producedUnisends its item, this item is discarded, and the originalfailureis forwarded downstream. If the producedUnifails, a composite failure containing both the original failure and the failure from the executed supplier is propagated downstream.- Parameters:
supplier- the supplier, must not benull- Returns:
- the new
Uni
-
transform
public Uni<T> transform(java.util.function.Function<? super java.lang.Throwable,? extends java.lang.Throwable> mapper)
Produces a newUniinvoking the given function when the currentUnipropagates a failure. The function can transform the received failure into another exception that will be fired as failure downstream.- Parameters:
mapper- the mapper function, must not benull, must not returnnull- Returns:
- the new
Uni
-
recoverWithItem
public Uni<T> recoverWithItem(T fallback)
Recovers from the received failure (matching the predicate if set) by using a fallback item.- Parameters:
fallback- the fallback, can benull- Returns:
- the new
Unithat would emit the given fallback in case the upstream sends us a failure.
-
recoverWithItem
public Uni<T> recoverWithItem(java.util.function.Supplier<T> supplier)
Recovers from the received failure (matching the predicate if set) by using a item generated by the given supplier. The supplier is called when the failure is received.If the supplier throws an exception, a
CompositeExceptioncontaining both the received failure and the thrown exception is propagated downstream.- Parameters:
supplier- the supplier providing the fallback item. Must not benull, can returnnull.- Returns:
- the new
Unithat would emit the produced item in case the upstream sends a failure.
-
recoverWithItem
public Uni<T> recoverWithItem(java.util.function.Function<? super java.lang.Throwable,? extends T> function)
Recovers from the received failure (matching the predicate if set) by using a item generated by the given function. The function is called when the failure is received.If the function throws an exception, a
CompositeExceptioncontaining both the received failure and the thrown exception is propagated downstream.- Parameters:
function- the function providing the fallback item. Must not benull, can returnnull.- Returns:
- the new
Unithat would emit the produced item in case the upstream sends a failure.
-
recoverWithUni
public Uni<T> recoverWithUni(java.util.function.Function<? super java.lang.Throwable,Uni<? extends T>> function)
Recovers from the received failure (matching the predicate if set) with anotherUni. Thisuniis produced by the given function. Thisunican emit an item (potentiallynullor a failure. The function must not returnnull.If the function throws an exception, a
CompositeExceptioncontaining both the received failure and the thrown exception is propagated downstream.- Parameters:
function- the function providing the fallback uni. Must not benull, must not returnnull.- Returns:
- the new
Unithat would emit events from the uni produced by the given function in case the upstream sends a failure.
-
recoverWithUni
public Uni<T> recoverWithUni(java.util.function.Supplier<Uni<? extends T>> supplier)
Recovers from the received failure (matching the predicate if set) with anotherUni. Thisuniis produced by the given supplier. Thisunican emit an item (potentiallynullor a failure. The supplier must not returnnull.If the supplier throws an exception, a
CompositeExceptioncontaining both the received failure and the thrown exception is propagated downstream.- Parameters:
supplier- the supplier providing the fallback uni. Must not benull, must not returnnull.- Returns:
- the new
Unithat would emits events from the uni produced by the given supplier in case the upstream sends a failure.
-
recoverWithUni
public Uni<T> recoverWithUni(Uni<? extends T> fallback)
Recovers from the received failure (matching the predicate if set) with anotherUni. Thisunican emit an item (potentiallynullor a failure.- Parameters:
fallback- the fallbakc uni, must not benull- Returns:
- the new
Unithat would emit events from the uni in case the upstream sends a failure.
-
retry
public UniRetry<T> retry()
Configures the retry strategy.- Returns:
- the object to configure the retry.
-
-