Search in sources :

Example 21 with CompositeException

use of rx.exceptions.CompositeException 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 22 with CompositeException

use of rx.exceptions.CompositeException 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)

Example 23 with CompositeException

use of rx.exceptions.CompositeException in project autorest-clientruntime-for-java by Azure.

the class ExternalChildResourceCollectionImpl method commitAsync.

/**
 * Commits the changes in the external child resource childCollection.
 * <p/>
 * This method returns an observable stream, its observer's onNext will be called for each successfully
 * committed resource followed by one call to 'onCompleted' or one call to 'onError' with a
 * {@link CompositeException } containing the list of exceptions where each exception describes the reason
 * for failure of a resource commit.
 *
 * @return the observable stream
 */
public Observable<FluentModelTImpl> commitAsync() {
    if (this.isPostRunMode) {
        return Observable.error(new IllegalStateException("commitAsync() cannot be invoked when 'post run' mode is enabled"));
    }
    final ExternalChildResourceCollectionImpl<FluentModelTImpl, FluentModelT, InnerModelT, ParentImplT, ParentT> self = this;
    List<FluentModelTImpl> items = new ArrayList<>();
    for (FluentModelTImpl item : this.childCollection.values()) {
        items.add(item);
    }
    final List<Throwable> exceptionsList = Collections.synchronizedList(new ArrayList<Throwable>());
    Observable<FluentModelTImpl> deleteStream = Observable.from(items).filter(new Func1<FluentModelTImpl, Boolean>() {

        @Override
        public Boolean call(FluentModelTImpl childResource) {
            return childResource.pendingOperation() == ExternalChildResourceImpl.PendingOperation.ToBeRemoved;
        }
    }).flatMap(new Func1<FluentModelTImpl, Observable<FluentModelTImpl>>() {

        @Override
        public Observable<FluentModelTImpl> call(final FluentModelTImpl childResource) {
            return childResource.deleteResourceAsync().map(new Func1<Void, FluentModelTImpl>() {

                @Override
                public FluentModelTImpl call(Void response) {
                    return childResource;
                }
            }).doOnNext(new Action1<FluentModelTImpl>() {

                @Override
                public void call(FluentModelTImpl childResource) {
                    childResource.setPendingOperation(ExternalChildResourceImpl.PendingOperation.None);
                    self.childCollection.remove(childResource.name());
                }
            }).onErrorResumeNext(new Func1<Throwable, Observable<FluentModelTImpl>>() {

                @Override
                public Observable<FluentModelTImpl> call(Throwable throwable) {
                    exceptionsList.add(throwable);
                    return Observable.empty();
                }
            });
        }
    });
    Observable<FluentModelTImpl> createStream = Observable.from(items).filter(new Func1<FluentModelTImpl, Boolean>() {

        @Override
        public Boolean call(FluentModelTImpl childResource) {
            return childResource.pendingOperation() == ExternalChildResourceImpl.PendingOperation.ToBeCreated;
        }
    }).flatMap(new Func1<FluentModelTImpl, Observable<FluentModelTImpl>>() {

        @Override
        public Observable<FluentModelTImpl> call(final FluentModelTImpl childResource) {
            return childResource.createResourceAsync().map(new Func1<FluentModelT, FluentModelTImpl>() {

                @Override
                public FluentModelTImpl call(FluentModelT fluentModelT) {
                    return childResource;
                }
            }).doOnNext(new Action1<FluentModelTImpl>() {

                @Override
                public void call(FluentModelTImpl fluentModelT) {
                    childResource.setPendingOperation(ExternalChildResourceImpl.PendingOperation.None);
                }
            }).onErrorResumeNext(new Func1<Throwable, Observable<? extends FluentModelTImpl>>() {

                @Override
                public Observable<FluentModelTImpl> call(Throwable throwable) {
                    self.childCollection.remove(childResource.name());
                    exceptionsList.add(throwable);
                    return Observable.empty();
                }
            });
        }
    });
    Observable<FluentModelTImpl> updateStream = Observable.from(items).filter(new Func1<FluentModelTImpl, Boolean>() {

        @Override
        public Boolean call(FluentModelTImpl childResource) {
            return childResource.pendingOperation() == ExternalChildResourceImpl.PendingOperation.ToBeUpdated;
        }
    }).flatMap(new Func1<FluentModelTImpl, Observable<FluentModelTImpl>>() {

        @Override
        public Observable<FluentModelTImpl> call(final FluentModelTImpl childResource) {
            return childResource.updateResourceAsync().map(new Func1<FluentModelT, FluentModelTImpl>() {

                @Override
                public FluentModelTImpl call(FluentModelT e) {
                    return childResource;
                }
            }).doOnNext(new Action1<FluentModelTImpl>() {

                @Override
                public void call(FluentModelTImpl childResource) {
                    childResource.setPendingOperation(ExternalChildResourceImpl.PendingOperation.None);
                }
            }).onErrorResumeNext(new Func1<Throwable, Observable<? extends FluentModelTImpl>>() {

                @Override
                public Observable<FluentModelTImpl> call(Throwable throwable) {
                    exceptionsList.add(throwable);
                    return Observable.empty();
                }
            });
        }
    });
    final PublishSubject<FluentModelTImpl> aggregatedErrorStream = PublishSubject.create();
    Observable<FluentModelTImpl> operationsStream = Observable.merge(deleteStream, createStream, updateStream).doOnTerminate(new Action0() {

        @Override
        public void call() {
            if (clearAfterCommit()) {
                self.childCollection.clear();
            }
            if (exceptionsList.isEmpty()) {
                aggregatedErrorStream.onCompleted();
            } else {
                aggregatedErrorStream.onError(new CompositeException(exceptionsList));
            }
        }
    });
    Observable<FluentModelTImpl> stream = Observable.concat(operationsStream, aggregatedErrorStream);
    return stream;
}
Also used : Action0(rx.functions.Action0) CompositeException(rx.exceptions.CompositeException) ArrayList(java.util.ArrayList) Observable(rx.Observable) Func1(rx.functions.Func1)

Aggregations

CompositeException (rx.exceptions.CompositeException)23 Test (org.junit.Test)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 MockResponse (okhttp3.mockwebserver.MockResponse)13 RxJavaErrorHandler (rx.plugins.RxJavaErrorHandler)13 OnErrorFailedException (rx.exceptions.OnErrorFailedException)7 ArrayList (java.util.ArrayList)6 Observable (rx.Observable)4 Func1 (rx.functions.Func1)4 Response (retrofit2.Response)3 TestSubscriber (rx.observers.TestSubscriber)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 GenericDebuggerRunner (com.intellij.debugger.impl.GenericDebuggerRunner)1 GenericDebuggerRunnerSettings (com.intellij.debugger.impl.GenericDebuggerRunnerSettings)1 ExecutionException (com.intellij.execution.ExecutionException)1 Executor (com.intellij.execution.Executor)1 ConfigurationInfoProvider (com.intellij.execution.configurations.ConfigurationInfoProvider)1 RemoteConnection (com.intellij.execution.configurations.RemoteConnection)1 RunProfile (com.intellij.execution.configurations.RunProfile)1 RunProfileState (com.intellij.execution.configurations.RunProfileState)1