Search in sources :

Example 1 with OnErrorFailedException

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

the class CompletableThrowingSafeSubscriberTest method bodyThrowingInOnErrorDeliveredToPlugin.

@Test
public void bodyThrowingInOnErrorDeliveredToPlugin() {
    server.enqueue(new MockResponse().setResponseCode(404));
    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);
                }
            }
        }
    });
    RecordingSubscriber<Void> observer = subscriberRule.create();
    final RuntimeException e = new RuntimeException();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    service.completable().subscribe(new ForwardingCompletableObserver(observer) {

        @Override
        public void onError(Throwable throwable) {
            errorRef.set(throwable);
            throw e;
        }
    });
    CompositeException composite = (CompositeException) pluginRef.get().getCause();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) CompositeException(rx.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) OnErrorFailedException(rx.exceptions.OnErrorFailedException) Test(org.junit.Test)

Example 2 with OnErrorFailedException

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

the class ObservableThrowingSafeSubscriberTest method responseThrowingInOnErrorDeliveredToPlugin.

@Test
public void responseThrowingInOnErrorDeliveredToPlugin() {
    server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AFTER_REQUEST));
    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);
                }
            }
        }
    });
    RecordingSubscriber<Response<String>> observer = subscriberRule.create();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final RuntimeException e = new RuntimeException();
    service.response().subscribe(new ForwardingSubscriber<Response<String>>(observer) {

        @Override
        public void onError(Throwable throwable) {
            if (!errorRef.compareAndSet(null, throwable)) {
                throw Exceptions.propagate(throwable);
            }
            throw e;
        }
    });
    CompositeException composite = (CompositeException) pluginRef.get().getCause();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : Response(retrofit2.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) CompositeException(rx.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) OnErrorFailedException(rx.exceptions.OnErrorFailedException) Test(org.junit.Test)

Example 3 with OnErrorFailedException

use of rx.exceptions.OnErrorFailedException in project okhttp-OkGo by jeasonlzy.

the class CallArbiter method emitResponse.

private void emitResponse(List<Response<T>> responseList) {
    try {
        synchronized (this) {
            Iterator<Response<T>> iterator = responseList.iterator();
            while (iterator.hasNext()) {
                Response<T> next = iterator.next();
                iterator.remove();
                if (!isUnsubscribed()) {
                    subscriber.onNext(next);
                } else {
                    return;
                }
            }
        }
    } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
        RxJavaHooks.getOnError().call(e);
    } catch (Throwable t) {
        Exceptions.throwIfFatal(t);
        try {
            subscriber.onError(t);
        } catch (OnCompletedFailedException | OnErrorFailedException | OnErrorNotImplementedException e) {
            RxJavaHooks.getOnError().call(e);
        } catch (Throwable inner) {
            Exceptions.throwIfFatal(inner);
            RxJavaHooks.getOnError().call(new CompositeException(t, inner));
        }
    }
}
Also used : Response(com.lzy.okgo.model.Response) OnCompletedFailedException(rx.exceptions.OnCompletedFailedException) CompositeException(rx.exceptions.CompositeException) OnErrorNotImplementedException(rx.exceptions.OnErrorNotImplementedException) OnErrorFailedException(rx.exceptions.OnErrorFailedException)

Example 4 with OnErrorFailedException

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

the class ObservableThrowingSafeSubscriberTest method resultThrowingInOnErrorDeliveredToPlugin.

@Test
public void resultThrowingInOnErrorDeliveredToPlugin() {
    server.enqueue(new MockResponse());
    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);
                }
            }
        }
    });
    RecordingSubscriber<Result<String>> observer = subscriberRule.create();
    final RuntimeException first = new RuntimeException();
    final RuntimeException second = new RuntimeException();
    service.result().subscribe(new ForwardingSubscriber<Result<String>>(observer) {

        @Override
        public void onNext(Result<String> value) {
            // The only way to trigger onError for a result is if onNext throws.
            throw first;
        }

        @Override
        public void onError(Throwable throwable) {
            throw second;
        }
    });
    CompositeException composite = (CompositeException) pluginRef.get().getCause();
    assertThat(composite.getExceptions()).containsExactly(first, second);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) CompositeException(rx.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) OnErrorFailedException(rx.exceptions.OnErrorFailedException) Test(org.junit.Test)

Example 5 with OnErrorFailedException

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

the class ObservableThrowingSafeSubscriberTest method bodyThrowingInOnErrorDeliveredToPlugin.

@Test
public void bodyThrowingInOnErrorDeliveredToPlugin() {
    server.enqueue(new MockResponse().setResponseCode(404));
    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);
                }
            }
        }
    });
    RecordingSubscriber<String> observer = subscriberRule.create();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final RuntimeException e = new RuntimeException();
    service.body().subscribe(new ForwardingSubscriber<String>(observer) {

        @Override
        public void onError(Throwable throwable) {
            if (!errorRef.compareAndSet(null, throwable)) {
                throw Exceptions.propagate(throwable);
            }
            throw e;
        }
    });
    CompositeException composite = (CompositeException) pluginRef.get().getCause();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) CompositeException(rx.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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