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);
});
});
}
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");
}
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);
}
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);
}
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));
}
}
Aggregations