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);
}
}
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);
}
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;
}
Aggregations