Search in sources :

Example 11 with CompositeException

use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.

the class ExceptionHelper method addThrowable.

public static <T> boolean addThrowable(AtomicReference<Throwable> field, Throwable exception) {
    for (; ; ) {
        Throwable current = field.get();
        if (current == TERMINATED) {
            return false;
        }
        Throwable update;
        if (current == null) {
            update = exception;
        } else {
            update = new CompositeException(current, exception);
        }
        if (field.compareAndSet(current, update)) {
            return true;
        }
    }
}
Also used : CompositeException(io.reactivex.exceptions.CompositeException)

Example 12 with CompositeException

use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.

the class ExceptionHelper method flatten.

/**
     * Returns a flattened list of Throwables from tree-like CompositeException chain.
     * @param t the starting throwable
     * @return the list of Throwables flattened in a depth-first manner
     */
public static List<Throwable> flatten(Throwable t) {
    List<Throwable> list = new ArrayList<Throwable>();
    ArrayDeque<Throwable> deque = new ArrayDeque<Throwable>();
    deque.offer(t);
    while (!deque.isEmpty()) {
        Throwable e = deque.removeFirst();
        if (e instanceof CompositeException) {
            CompositeException ce = (CompositeException) e;
            List<Throwable> exceptions = ce.getExceptions();
            for (int i = exceptions.size() - 1; i >= 0; i--) {
                deque.offerFirst(exceptions.get(i));
            }
        } else {
            list.add(e);
        }
    }
    return list;
}
Also used : CompositeException(io.reactivex.exceptions.CompositeException)

Example 13 with CompositeException

use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.

the class BaseTestConsumer method fail.

/**
     * Fail with the given message and add the sequence of errors as suppressed ones.
     * <p>Note this is deliberately the only fail method. Most of the times an assertion
     * would fail but it is possible it was due to an exception somewhere. This construct
     * will capture those potential errors and report it along with the original failure.
     *
     * @param message the message to use
     * @return AssertionError the prepared AssertionError instance
     */
protected final AssertionError fail(String message) {
    StringBuilder b = new StringBuilder(64 + message.length());
    b.append(message);
    b.append(" (").append("latch = ").append(done.getCount()).append(", ").append("values = ").append(values.size()).append(", ").append("errors = ").append(errors.size()).append(", ").append("completions = ").append(completions);
    if (timeout) {
        b.append(", timeout!");
    }
    if (isDisposed()) {
        b.append(", disposed!");
    }
    CharSequence tag = this.tag;
    if (tag != null) {
        b.append(", tag = ").append(tag);
    }
    b.append(')');
    AssertionError ae = new AssertionError(b.toString());
    if (!errors.isEmpty()) {
        if (errors.size() == 1) {
            ae.initCause(errors.get(0));
        } else {
            CompositeException ce = new CompositeException(errors);
            ae.initCause(ce);
        }
    }
    return ae;
}
Also used : CompositeException(io.reactivex.exceptions.CompositeException)

Example 14 with CompositeException

use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.

the class CompositeDisposableTest method testCompositeException.

@Test
public void testCompositeException() {
    final AtomicInteger counter = new AtomicInteger();
    CompositeDisposable s = new CompositeDisposable();
    s.add(Disposables.fromRunnable(new Runnable() {

        @Override
        public void run() {
            throw new RuntimeException("failed on first one");
        }
    }));
    s.add(Disposables.fromRunnable(new Runnable() {

        @Override
        public void run() {
            throw new RuntimeException("failed on second one too");
        }
    }));
    s.add(Disposables.fromRunnable(new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    try {
        s.dispose();
        fail("Expecting an exception");
    } catch (CompositeException e) {
        // we expect this
        assertEquals(e.getExceptions().size(), 2);
    }
    // we should still have disposed to the second one
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeException(io.reactivex.exceptions.CompositeException) Test(org.junit.Test)

Example 15 with CompositeException

use of io.reactivex.exceptions.CompositeException in project retrofit by square.

the class ObservableThrowingTest method responseThrowingInOnErrorDeliveredToPlugin.

@Test
public void responseThrowingInOnErrorDeliveredToPlugin() {
    server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AFTER_REQUEST));
    final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            if (!throwableRef.compareAndSet(null, throwable)) {
                throw Exceptions.propagate(throwable);
            }
        }
    });
    RecordingObserver<Response<String>> observer = subscriberRule.create();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final RuntimeException e = new RuntimeException();
    service.response().subscribe(new ForwardingObserver<Response<String>>(observer) {

        @Override
        public void onError(Throwable throwable) {
            if (!errorRef.compareAndSet(null, throwable)) {
                throw Exceptions.propagate(throwable);
            }
            throw e;
        }
    });
    // noinspection ThrowableResultOfMethodCallIgnored
    CompositeException composite = (CompositeException) throwableRef.get();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : Response(retrofit2.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) CompositeException(io.reactivex.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompositeException(io.reactivex.exceptions.CompositeException) Test(org.junit.Test)

Aggregations

CompositeException (io.reactivex.exceptions.CompositeException)21 Test (org.junit.Test)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 MockResponse (okhttp3.mockwebserver.MockResponse)14 Response (retrofit2.Response)4 Ignore (org.junit.Ignore)2 ParseException (android.net.ParseException)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSerializer (com.google.gson.JsonSerializer)1 Single (io.reactivex.Single)1 TestObserver (io.reactivex.observers.TestObserver)1 RxCacheException (io.rx_cache2.RxCacheException)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 Runner (io.vertx.example.util.Runner)1 AbstractVerticle (io.vertx.reactivex.core.AbstractVerticle)1 JDBCClient (io.vertx.reactivex.ext.jdbc.JDBCClient)1 IOException (java.io.IOException)1 NotSerializableException (java.io.NotSerializableException)1 ConnectException (java.net.ConnectException)1