Search in sources :

Example 1 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class MultiOnFailure method call.

/**
 * Produces a new {@link Multi} invoking the given @{code action} when a {@code failure} event is received.
 * <p>
 * Unlike {@link #invoke(Consumer)}, the passed function returns a {@link Uni}. When the produced {@code Uni} sends
 * its result, the result is discarded, and the original {@code failure} is forwarded downstream. If the produced
 * {@code Uni} fails, a {@link io.smallrye.mutiny.CompositeException} composed by the original failure and the
 * caught failure is propagated downstream.
 * <p>
 * If the asynchronous action throws an exception, this exception is propagated downstream as a
 * {@link io.smallrye.mutiny.CompositeException} composed with the original failure and the caught exception.
 * <p>
 * This method preserves the order of the items, meaning that the downstream received the items in the same order
 * as the upstream has emitted them.
 *
 * @param action the function taking the failure and returning a {@link Uni}, must not be {@code null}
 * @return the new {@link Multi}
 */
@CheckReturnValue
public Multi<T> call(Function<Throwable, Uni<?>> action) {
    Function<Throwable, Uni<?>> actual = Infrastructure.decorate(nonNull(action, "action"));
    return recoverWithMulti(failure -> {
        Uni<?> uni = actual.apply(failure);
        if (uni == null) {
            throw new NullPointerException("The `action` produced a `null` Uni");
        }
        return Multi.createFrom().emitter(emitter -> {
            Cancellable cancellable = uni.subscribe().with(success -> emitter.fail(failure), subFailure -> emitter.fail(new CompositeException(failure, subFailure)));
            emitter.onTermination(cancellable::cancel);
        });
    });
}
Also used : Uni(io.smallrye.mutiny.Uni) Cancellable(io.smallrye.mutiny.subscription.Cancellable) CompositeException(io.smallrye.mutiny.CompositeException) CheckReturnValue(io.smallrye.common.annotation.CheckReturnValue)

Example 2 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class MultiConcatTest method testWithFailureCollection.

@Test
public void testWithFailureCollection() {
    IllegalStateException boom = new IllegalStateException("boom");
    IllegalStateException boom2 = new IllegalStateException("boom2");
    AssertSubscriber<Integer> subscriber = Multi.createBy().concatenating().collectFailures().streams(Multi.createFrom().item(5), Multi.createFrom().failure(boom), Multi.createFrom().item(6), Multi.createFrom().failure(boom2)).subscribe().withSubscriber(new AssertSubscriber<>(5));
    subscriber.assertTerminated().assertItems(5, 6).assertFailedWith(CompositeException.class, "boom").assertFailedWith(CompositeException.class, "boom2");
    assertThat(subscriber.getFailure()).isInstanceOf(CompositeException.class);
    CompositeException ce = (CompositeException) subscriber.getFailure();
    assertThat(ce.getCauses()).hasSize(2);
    subscriber = Multi.createBy().concatenating().streams(Multi.createFrom().item(5), Multi.createFrom().failure(boom), Multi.createFrom().item(6), Multi.createFrom().failure(boom)).subscribe().withSubscriber(new AssertSubscriber<>(5));
    subscriber.assertTerminated().assertItems(5).assertFailedWith(IllegalStateException.class, "boom");
}
Also used : CompositeException(io.smallrye.mutiny.CompositeException) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Test(org.junit.jupiter.api.Test)

Example 3 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class SubscriptionsTest method addFailures.

@Test
public void addFailures() {
    AtomicReference<Throwable> container = new AtomicReference<>();
    IOException boom = new IOException("boom");
    IllegalStateException ise = new IllegalStateException("boom");
    NullPointerException npe = new NullPointerException("boom!");
    assertThat(Subscriptions.addFailure(container, boom)).isTrue();
    assertThat(container.get()).isEqualTo(boom);
    assertThat(Subscriptions.addFailure(container, ise)).isTrue();
    assertThat(container.get()).isInstanceOf(CompositeException.class);
    CompositeException ce = (CompositeException) container.get();
    assertThat(ce.getCauses()).containsExactly(boom, ise);
    assertThat(Subscriptions.addFailure(container, npe)).isTrue();
    assertThat(container.get()).isInstanceOf(CompositeException.class);
    ce = (CompositeException) container.get();
    assertThat(ce.getCauses()).containsExactly(boom, ise, npe);
    assertThat(Subscriptions.markFailureAsTerminated(container)).isInstanceOf(CompositeException.class);
    assertThat(Subscriptions.addFailure(container, boom)).isFalse();
    assertThat(container).hasValue(Subscriptions.TERMINATED);
}
Also used : CompositeException(io.smallrye.mutiny.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 4 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class ResourceMulti method subscribe.

@Override
public void subscribe(MultiSubscriber<? super I> subscriber) {
    R resource;
    try {
        resource = resourceSupplier.get();
        if (resource == null) {
            throw new IllegalArgumentException(ParameterValidation.SUPPLIER_PRODUCED_NULL);
        }
    } catch (Throwable e) {
        Subscriptions.fail(subscriber, e);
        return;
    }
    Publisher<? extends I> stream;
    try {
        stream = streamSupplier.apply(resource);
        if (stream == null) {
            throw new IllegalArgumentException(ParameterValidation.SUPPLIER_PRODUCED_NULL);
        }
    } catch (Throwable e) {
        try {
            Uni<Void> uni = onFailure.apply(resource, e);
            if (uni == null) {
                Subscriptions.fail(subscriber, new NullPointerException("Unable to call the finalizer - it returned `null`"));
            } else {
                uni.subscribe().with(completed -> Subscriptions.fail(subscriber, e), failed -> Subscriptions.fail(subscriber, new CompositeException(e, failed)));
            }
        } catch (Throwable ex) {
            Subscriptions.fail(subscriber, new CompositeException(e, ex));
            return;
        }
        Subscriptions.fail(subscriber, e);
        return;
    }
    ResourceSubscriber<I, R> us = new ResourceSubscriber<>(subscriber, resource, onCompletion, onFailure, onCancellation);
    stream.subscribe(us);
}
Also used : Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) AbstractMulti(io.smallrye.mutiny.operators.AbstractMulti) ContextSupport(io.smallrye.mutiny.subscription.ContextSupport) BiFunction(java.util.function.BiFunction) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Context(io.smallrye.mutiny.Context) Uni(io.smallrye.mutiny.Uni) Subscriptions(io.smallrye.mutiny.helpers.Subscriptions) CompositeException(io.smallrye.mutiny.CompositeException) ParameterValidation(io.smallrye.mutiny.helpers.ParameterValidation) Subscription(org.reactivestreams.Subscription) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) Uni(io.smallrye.mutiny.Uni) CompositeException(io.smallrye.mutiny.CompositeException)

Example 5 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class SafeSubscriber method onError.

@Override
public void onError(Throwable t) {
    Objects.requireNonNull(t);
    if (done) {
        return;
    }
    done = true;
    if (upstream == null) {
        Throwable npe = new NullPointerException("Subscription not set!");
        try {
            downstream.onSubscribe(Subscriptions.empty());
        } catch (Throwable e) {
            // can't call onError because the actual's state may be corrupt at this point
            Infrastructure.handleDroppedException(new CompositeException(t, e));
            return;
        }
        try {
            downstream.onError(new CompositeException(t, npe));
        } catch (Throwable e) {
            // nothing we can do.
            Infrastructure.handleDroppedException(new CompositeException(t, npe, e));
        }
        return;
    }
    try {
        downstream.onError(t);
    } catch (Throwable ex) {
        // nothing we can do.
        Infrastructure.handleDroppedException(new CompositeException(t, ex));
    }
}
Also used : CompositeException(io.smallrye.mutiny.CompositeException)

Aggregations

CompositeException (io.smallrye.mutiny.CompositeException)20 Test (org.junit.jupiter.api.Test)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 IOException (java.io.IOException)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 RepeatedTest (org.junit.jupiter.api.RepeatedTest)8 Subscription (org.reactivestreams.Subscription)8 Uni (io.smallrye.mutiny.Uni)7 Cancellable (io.smallrye.mutiny.subscription.Cancellable)5 Function (java.util.function.Function)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)4 Consumer (java.util.function.Consumer)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 CheckReturnValue (io.smallrye.common.annotation.CheckReturnValue)1 Context (io.smallrye.mutiny.Context)1 Multi (io.smallrye.mutiny.Multi)1