Search in sources :

Example 6 with OnErrorFailedException

use of rx.exceptions.OnErrorFailedException in project retrofit by square.

the class CallArbiter method deliverResponse.

private void deliverResponse(Response<T> response) {
    try {
        if (!isUnsubscribed()) {
            subscriber.onNext(response);
        }
    } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
        RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
        return;
    } catch (Throwable t) {
        Exceptions.throwIfFatal(t);
        try {
            subscriber.onError(t);
        } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
            RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
        } catch (Throwable inner) {
            Exceptions.throwIfFatal(inner);
            CompositeException composite = new CompositeException(t, inner);
            RxJavaPlugins.getInstance().getErrorHandler().handleError(composite);
        }
        return;
    }
    try {
        if (!isUnsubscribed()) {
            subscriber.onCompleted();
        }
    } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
        RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
    } catch (Throwable t) {
        Exceptions.throwIfFatal(t);
        RxJavaPlugins.getInstance().getErrorHandler().handleError(t);
    }
}
Also used : OnCompletedFailedException(rx.exceptions.OnCompletedFailedException) CompositeException(rx.exceptions.CompositeException) OnErrorNotImplementedException(rx.exceptions.OnErrorNotImplementedException) OnErrorFailedException(rx.exceptions.OnErrorFailedException)

Example 7 with OnErrorFailedException

use of rx.exceptions.OnErrorFailedException in project retrofit by square.

the class AsyncTest method bodyThrowingInOnSafeSubscriberErrorDeliveredToPlugin.

@Test
public void bodyThrowingInOnSafeSubscriberErrorDeliveredToPlugin() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> pluginRef = new AtomicReference<>();
    RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {

        @Override
        public void handleError(Throwable throwable) {
            if (throwable instanceof OnErrorFailedException) {
                if (!pluginRef.compareAndSet(null, throwable)) {
                    // Don't swallow secondary errors!
                    throw Exceptions.propagate(throwable);
                }
                latch.countDown();
            }
        }
    });
    final TestSubscriber<Void> subscriber = new TestSubscriber<>();
    final RuntimeException e = new RuntimeException();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    service.completable().subscribe(new AsyncCompletableSubscriber() {

        @Override
        public void onCompleted() {
            subscriber.onCompleted();
        }

        @Override
        public void onError(Throwable t) {
            errorRef.set(t);
            throw e;
        }
    });
    assertTrue(latch.await(1, SECONDS));
    OnErrorFailedException failed = (OnErrorFailedException) pluginRef.get();
    CompositeException composite = (CompositeException) failed.getCause();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) AsyncCompletableSubscriber(rx.observers.AsyncCompletableSubscriber) CompositeException(rx.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestSubscriber(rx.observers.TestSubscriber) OnErrorFailedException(rx.exceptions.OnErrorFailedException) Test(org.junit.Test)

Aggregations

CompositeException (rx.exceptions.CompositeException)7 OnErrorFailedException (rx.exceptions.OnErrorFailedException)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 Test (org.junit.Test)5 RxJavaErrorHandler (rx.plugins.RxJavaErrorHandler)5 OnCompletedFailedException (rx.exceptions.OnCompletedFailedException)2 OnErrorNotImplementedException (rx.exceptions.OnErrorNotImplementedException)2 Response (com.lzy.okgo.model.Response)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Response (retrofit2.Response)1 AsyncCompletableSubscriber (rx.observers.AsyncCompletableSubscriber)1 TestSubscriber (rx.observers.TestSubscriber)1