Class UniOnFailure<T>

  • Type Parameters:
    T - the type of item

    public class UniOnFailure<T>
    extends java.lang.Object
    Configures 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)
      Produces a new Uni invoking the given function when the current Uni propagates a failure (matching the predicate if set).
      Uni<T> call​(java.util.function.Supplier<Uni<?>> supplier)
      Produces a new Uni invoking the given supplier when the current Uni propagates a failure (matching the predicate if set).
      Uni<T> invoke​(java.lang.Runnable callback)
      Produces a new Uni invoking the given callback when this Uni emits a failure (matching the predicate if set).
      Uni<T> invoke​(java.util.function.Consumer<java.lang.Throwable> callback)
      Produces a new Uni invoking the given callback when this Uni emits a failure (matching the predicate if set).
      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 a null item in the resulting Uni.
      Uni<T> recoverWithUni​(Uni<? extends T> fallback)
      Recovers from the received failure (matching the predicate if set) with another Uni.
      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 another Uni.
      Uni<T> recoverWithUni​(java.util.function.Supplier<Uni<? extends T>> supplier)
      Recovers from the received failure (matching the predicate if set) with another Uni.
      UniRetry<T> retry()
      Configures the retry strategy.
      Uni<T> transform​(java.util.function.Function<? super java.lang.Throwable,​? extends java.lang.Throwable> mapper)
      Produces a new Uni invoking the given function when the current Uni propagates a failure.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • UniOnFailure

        public UniOnFailure​(Uni<T> upstream,
                            java.util.function.Predicate<? super java.lang.Throwable> predicate)
    • Method Detail

      • invoke

        @CheckReturnValue
        public Uni<T> invoke​(java.util.function.Consumer<java.lang.Throwable> callback)
        Produces a new Uni invoking the given callback when this Uni emits a failure (matching the predicate if set).

        If the callback throws an exception, a CompositeException is propagated downstream. This exception is composed by the received failure and the thrown exception.

        Parameters:
        callback - the callback, must not be null
        Returns:
        the new Uni
      • invoke

        @CheckReturnValue
        public Uni<T> invoke​(java.lang.Runnable callback)
        Produces a new Uni invoking the given callback when this Uni emits a failure (matching the predicate if set).

        If the callback throws an exception, a CompositeException is propagated downstream. This exception is composed by the received failure and the thrown exception.

        Parameters:
        callback - the callback, must not be null
        Returns:
        the new Uni
      • call

        @CheckReturnValue
        public Uni<T> call​(java.util.function.Function<java.lang.Throwable,​Uni<?>> action)
        Produces a new Uni invoking the given function when the current Uni propagates 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 new Uni invoking the given @{code action} when the failure event is received.

        Unlike invoke(Consumer), the passed function returns a Uni. When the produced Uni sends its item, this item is discarded, and the original failure is forwarded downstream. If the produced Uni fails, a composite failure containing both the original failure and the failure from the executed action is propagated downstream.

        Parameters:
        action - the callback, must not be null
        Returns:
        the new Uni
      • call

        @CheckReturnValue
        public Uni<T> call​(java.util.function.Supplier<Uni<?>> supplier)
        Produces a new Uni invoking the given supplier when the current Uni propagates a failure (matching the predicate if set). The supplier ignores the failure. Produces a new Uni invoking the given @{code supplier} when the failure event is received.

        Unlike invoke(Consumer), the passed function returns a Uni. When the produced Uni sends its item, this item is discarded, and the original failure is forwarded downstream. If the produced Uni fails, a composite failure containing both the original failure and the failure from the executed supplier is propagated downstream.

        Parameters:
        supplier - the supplier, must not be null
        Returns:
        the new Uni
      • transform

        @CheckReturnValue
        public Uni<T> transform​(java.util.function.Function<? super java.lang.Throwable,​? extends java.lang.Throwable> mapper)
        Produces a new Uni invoking the given function when the current Uni propagates 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 be null, must not return null
        Returns:
        the new Uni
      • recoverWithItem

        @CheckReturnValue
        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 be null
        Returns:
        the new Uni that would emit the given fallback in case the upstream sends us a failure.
      • recoverWithItem

        @CheckReturnValue
        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 CompositeException containing both the received failure and the thrown exception is propagated downstream.

        Parameters:
        supplier - the supplier providing the fallback item. Must not be null, can return null.
        Returns:
        the new Uni that would emit the produced item in case the upstream sends a failure.
      • recoverWithItem

        @CheckReturnValue
        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 CompositeException containing both the received failure and the thrown exception is propagated downstream.

        Parameters:
        function - the function providing the fallback item. Must not be null, can return null.
        Returns:
        the new Uni that would emit the produced item in case the upstream sends a failure.
      • recoverWithUni

        @CheckReturnValue
        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 another Uni. This uni is produced by the given function. This uni can emit an item (potentially null or a failure. The function must not return null.

        If the function throws an exception, a CompositeException containing both the received failure and the thrown exception is propagated downstream.

        Parameters:
        function - the function providing the fallback uni. Must not be null, must not return null.
        Returns:
        the new Uni that would emit events from the uni produced by the given function in case the upstream sends a failure.
      • recoverWithUni

        @CheckReturnValue
        public Uni<T> recoverWithUni​(java.util.function.Supplier<Uni<? extends T>> supplier)
        Recovers from the received failure (matching the predicate if set) with another Uni. This uni is produced by the given supplier. This uni can emit an item (potentially null or a failure. The supplier must not return null.

        If the supplier throws an exception, a CompositeException containing both the received failure and the thrown exception is propagated downstream.

        Parameters:
        supplier - the supplier providing the fallback uni. Must not be null, must not return null.
        Returns:
        the new Uni that would emits events from the uni produced by the given supplier in case the upstream sends a failure.
      • recoverWithUni

        @CheckReturnValue
        public Uni<T> recoverWithUni​(Uni<? extends T> fallback)
        Recovers from the received failure (matching the predicate if set) with another Uni. This uni can emit an item (potentially null or a failure.
        Parameters:
        fallback - the fallbakc uni, must not be null
        Returns:
        the new Uni that would emit events from the uni in case the upstream sends a failure.
      • retry

        @CheckReturnValue
        public UniRetry<T> retry()
        Configures the retry strategy.
        Returns:
        the object to configure the retry.
      • recoverWithNull

        @CheckReturnValue
        public Uni<T> recoverWithNull()
        Recovers from the received failure by ignoring it and emitting a null item in the resulting Uni.
        Returns:
        the new Uni that emits null on failure