Search in sources :

Example 1 with FilteredExecutionPathException

use of com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException in project cyclops by aol.

the class LazySimpleReactStream method onFail.

/**
 * Recover for a particular class of exceptions only. Chain onFail methods from specific Exception classes
 * to general, as Exceptions will be caught and handled in order.
 * e.g.
 * <pre>
 * {@code
 *      			onFail(IOException.class, recoveryFunction1)
 *      			.onFail(Throwable.class,recovertyFunction2)
 *  }
 * </pre>
 * For an IOException recoveryFunction1 will be executed
 *
 * but with the definitions reveresed
 * <pre>
 *      {@code
 *      	onFail(Throwable.class,recovertyFunction2)
 *      		.onFail(IOException.class, recoveryFunction1)
 *     	}
 *     	</pre>
 *
 * recoveryFunction1 will not be called
 *
 * @param exceptionClass Class of exceptions to recover from
 * @param fn Recovery function
 * @return recovery value
 */
@Override
default LazySimpleReactStream<U> onFail(final Class<? extends Throwable> exceptionClass, final Function<? super SimpleReactFailedStageException, ? extends U> fn) {
    final Function<PipelineBuilder, PipelineBuilder> mapper = (ft) -> ft.exceptionally((t) -> {
        if (t instanceof FilteredExecutionPathException)
            throw (FilteredExecutionPathException) t;
        Throwable throwable = t;
        if (t instanceof CompletionException)
            throwable = ((Exception) t).getCause();
        // exceptions from initial supplier won't be wrapper in SimpleReactFailedStageException
        final SimpleReactFailedStageException simpleReactException = assureSimpleReactException(throwable);
        if (exceptionClass.isAssignableFrom(simpleReactException.getCause().getClass()))
            return fn.apply(simpleReactException);
        throw simpleReactException;
    });
    return this.withLastActive(getLastActive().operation(mapper));
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Iterator(java.util.Iterator) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Continueable(com.oath.cyclops.react.async.subscription.Continueable) Executor(java.util.concurrent.Executor) LazyReact(cyclops.futurestream.LazyReact) Streams(cyclops.companion.Streams) Spliterators(java.util.Spliterators) Predicate(java.util.function.Predicate) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Function(java.util.function.Function) Consumer(java.util.function.Consumer) ReactiveSeq(cyclops.reactive.ReactiveSeq) Stream(java.util.stream.Stream) LazyStreamWrapper(com.oath.cyclops.internal.react.stream.LazyStreamWrapper) FilteredExecutionPathException(com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException) Optional(java.util.Optional) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) StreamSupport(java.util.stream.StreamSupport) QueueFactory(com.oath.cyclops.async.adapters.QueueFactory) Spliterator(java.util.Spliterator) PipelineBuilder(com.oath.cyclops.internal.react.async.future.PipelineBuilder) CompletionException(java.util.concurrent.CompletionException) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) FilteredExecutionPathException(com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException) PipelineBuilder(com.oath.cyclops.internal.react.async.future.PipelineBuilder) CompletionException(java.util.concurrent.CompletionException) FilteredExecutionPathException(com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException)

Example 2 with FilteredExecutionPathException

use of com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException in project cyclops by aol.

the class Runner method handleFilter.

private <T> void handleFilter(final Continuation[] cont, final FastFuture<T> f) {
    final AtomicInteger called = new AtomicInteger(0);
    f.essential(event -> {
        if (event.exception != null && event.exception.getCause() instanceof FilteredExecutionPathException) {
            if (called.compareAndSet(0, 1))
                cont[0].proceed();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FilteredExecutionPathException(com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException)

Aggregations

FilteredExecutionPathException (com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException)2 ReactiveConvertableSequence (com.oath.cyclops.ReactiveConvertableSequence)1 QueueFactory (com.oath.cyclops.async.adapters.QueueFactory)1 FastFuture (com.oath.cyclops.internal.react.async.future.FastFuture)1 PipelineBuilder (com.oath.cyclops.internal.react.async.future.PipelineBuilder)1 LazyStreamWrapper (com.oath.cyclops.internal.react.stream.LazyStreamWrapper)1 SimpleReactFailedStageException (com.oath.cyclops.react.SimpleReactFailedStageException)1 Continueable (com.oath.cyclops.react.async.subscription.Continueable)1 Streams (cyclops.companion.Streams)1 LazyReact (cyclops.futurestream.LazyReact)1 ReactiveSeq (cyclops.reactive.ReactiveSeq)1 ListX (cyclops.reactive.collections.mutable.ListX)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Spliterator (java.util.Spliterator)1 Spliterators (java.util.Spliterators)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 Executor (java.util.concurrent.Executor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1