Search in sources :

Example 1 with SimpleReactFailedStageException

use of com.oath.cyclops.react.SimpleReactFailedStageException 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 SimpleReactFailedStageException

use of com.oath.cyclops.react.SimpleReactFailedStageException in project cyclops by aol.

the class CaptureTest method capture.

@Test
public void capture() throws InterruptedException {
    t = null;
    new SimpleReact().of("hello", "world").capture(e -> t = e).peek(System.out::println).then(this::exception).peek(System.out::println).then(s -> "hello" + s).run();
    Thread.sleep(500);
    assertNotNull(t);
    assertFalse(t.toString(), t instanceof SimpleReactFailedStageException);
    assertTrue(t.toString(), t instanceof InternalException);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyReact(cyclops.futurestream.LazyReact) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertNotNull(org.junit.Assert.assertNotNull) SimpleReact(cyclops.futurestream.SimpleReact) Assert.assertTrue(org.junit.Assert.assertTrue) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) Test(org.junit.Test) Assert.assertEquals(org.junit.Assert.assertEquals) SimpleReact(cyclops.futurestream.SimpleReact) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) Test(org.junit.Test)

Example 3 with SimpleReactFailedStageException

use of com.oath.cyclops.react.SimpleReactFailedStageException in project cyclops by aol.

the class CaptureTest method captureLazy.

@Test
public void captureLazy() {
    t = null;
    new LazyReact().of("hello", "world").capture(e -> t = e).peek(System.out::println).then(this::exception).forEach(System.out::println);
    assertNotNull(t);
    assertFalse(t.toString(), t instanceof SimpleReactFailedStageException);
    assertTrue(t.toString(), t instanceof InternalException);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyReact(cyclops.futurestream.LazyReact) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertNotNull(org.junit.Assert.assertNotNull) SimpleReact(cyclops.futurestream.SimpleReact) Assert.assertTrue(org.junit.Assert.assertTrue) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) Test(org.junit.Test) Assert.assertEquals(org.junit.Assert.assertEquals) LazyReact(cyclops.futurestream.LazyReact) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) Test(org.junit.Test)

Example 4 with SimpleReactFailedStageException

use of com.oath.cyclops.react.SimpleReactFailedStageException in project cyclops by aol.

the class CaptureTest method captureLast.

@Test
public void captureLast() throws InterruptedException {
    t = null;
    new SimpleReact().of("hello", "world").capture(e -> t = e).peek(System.out::println).peek(System.out::println).then(s -> "hello" + s).then(this::exception).run();
    Thread.sleep(500);
    assertNotNull(t);
    assertFalse(t.toString(), t instanceof SimpleReactFailedStageException);
    assertTrue(t.toString(), t instanceof InternalException);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyReact(cyclops.futurestream.LazyReact) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertNotNull(org.junit.Assert.assertNotNull) SimpleReact(cyclops.futurestream.SimpleReact) Assert.assertTrue(org.junit.Assert.assertTrue) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) Test(org.junit.Test) Assert.assertEquals(org.junit.Assert.assertEquals) SimpleReact(cyclops.futurestream.SimpleReact) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) Test(org.junit.Test)

Aggregations

SimpleReactFailedStageException (com.oath.cyclops.react.SimpleReactFailedStageException)4 LazyReact (cyclops.futurestream.LazyReact)4 SimpleReact (cyclops.futurestream.SimpleReact)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 Assert.assertNotNull (org.junit.Assert.assertNotNull)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Test (org.junit.Test)3 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 FilteredExecutionPathException (com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException)1 LazyStreamWrapper (com.oath.cyclops.internal.react.stream.LazyStreamWrapper)1 Continueable (com.oath.cyclops.react.async.subscription.Continueable)1 Streams (cyclops.companion.Streams)1 ReactiveSeq (cyclops.reactive.ReactiveSeq)1 ListX (cyclops.reactive.collections.mutable.ListX)1 Iterator (java.util.Iterator)1