use of rx.exceptions.CompositeException in project retrofit by square.
the class SingleThrowingTest 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 (!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 ForwardingObserver<String>(observer) {
@Override
public void onError(Throwable throwable) {
if (!errorRef.compareAndSet(null, throwable)) {
throw Exceptions.propagate(throwable);
}
throw e;
}
});
CompositeException composite = (CompositeException) pluginRef.get();
assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
use of rx.exceptions.CompositeException in project retrofit by square.
the class AsyncTest method bodyThrowingInOnErrorDeliveredToPlugin.
@Test
public void bodyThrowingInOnErrorDeliveredToPlugin() 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 (!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().unsafeSubscribe(new AsyncCompletableSubscriber() {
@Override
public void onCompleted() {
subscriber.onCompleted();
}
@Override
public void onError(Throwable t) {
errorRef.set(t);
throw e;
}
});
assertTrue(latch.await(1, SECONDS));
CompositeException composite = (CompositeException) pluginRef.get();
assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
use of rx.exceptions.CompositeException 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));
}
}
}
use of rx.exceptions.CompositeException in project autorest-clientruntime-for-java by Azure.
the class DAGErrorTests method testCompositeError.
@Test
public void testCompositeError() {
// Terminate on error strategy used in this task group is
// TaskGroupTerminateOnErrorStrategy::TERMINATE_ON_IN_PROGRESS_TASKS_COMPLETION
// Tasks marked X (B & G) will fault. B and G are not depends on each other.
// If B start at time 't0'th ms then G starts ~'t1 = (t0 + 250)'th ms.
// After B start, it asynchronously wait and emit an error at time '(t0 + 3500)' ms.
// In this setup, G gets ~3250 ms to start before B emits error. Eventually G also
// emit error.
// The final stream, emits result of all tasks that B and G directly or indirectly
// depends on and terminate with composite exception (that composes exception from
// B and G).
/**
* |--------->[M](0)
* |
* |==============>[J](1)----->[N](0)
* X |
* |------------------>[D](4)-->[B](3)--------------->[A](2)======================>[K](0)
* | ^ ^
* | | |
* [F](6)---->[E](5)--------------| |
* | | X |
* | |------->[G](4)-->[C](3)------------------
* | |
* | |============================>[L](2)---------->[P](1)=====>[Q](0)
* |
* |------------------------------------------------------------------>[H](1)----->[I](0)
*/
PancakeImpl pancakeM = new PancakeImpl("M", 250);
PancakeImpl pancakeN = new PancakeImpl("N", 250);
PancakeImpl pancakeK = new PancakeImpl("K", 250);
PancakeImpl pancakeQ = new PancakeImpl("Q", 250);
PancakeImpl pancakeI = new PancakeImpl("I", 250);
PancakeImpl pancakeJ = new PancakeImpl("J", 250);
pancakeJ.withInstantPancake(pancakeM);
pancakeJ.withInstantPancake(pancakeN);
PancakeImpl pancakeP = new PancakeImpl("P", 250);
pancakeP.withDelayedPancake(pancakeQ);
PancakeImpl pancakeH = new PancakeImpl("H", 250);
pancakeH.withInstantPancake(pancakeI);
PancakeImpl pancakeA = new PancakeImpl("A", 250);
PancakeImpl pancakeL = new PancakeImpl("L", 250);
pancakeL.withInstantPancake(pancakeP);
// Task B wait for 3500 ms then emit error
PancakeImpl pancakeB = new PancakeImpl("B", 3500, true);
pancakeB.withInstantPancake(pancakeA);
PancakeImpl pancakeC = new PancakeImpl("C", 250);
pancakeC.withInstantPancake(pancakeA);
PancakeImpl pancakeD = new PancakeImpl("D", 250);
pancakeD.withInstantPancake(pancakeB);
// Task G wait for 250 ms then emit error
PancakeImpl pancakeG = new PancakeImpl("G", 250, true);
pancakeG.withInstantPancake(pancakeC);
pancakeG.withDelayedPancake(pancakeL);
PancakeImpl pancakeE = new PancakeImpl("E", 250);
pancakeE.withInstantPancake(pancakeB);
pancakeE.withInstantPancake(pancakeG);
PancakeImpl pancakeF = new PancakeImpl("F", 250);
pancakeF.withInstantPancake(pancakeD);
pancakeF.withInstantPancake(pancakeE);
pancakeF.withInstantPancake(pancakeH);
pancakeA.withDelayedPancake(pancakeJ);
pancakeA.withDelayedPancake(pancakeK);
final Set<String> expectedToSee = new TreeSet<>();
expectedToSee.add("M");
expectedToSee.add("N");
expectedToSee.add("K");
expectedToSee.add("Q");
expectedToSee.add("I");
expectedToSee.add("J");
expectedToSee.add("P");
expectedToSee.add("H");
expectedToSee.add("A");
expectedToSee.add("L");
expectedToSee.add("C");
final Set<String> seen = new TreeSet<>();
final List<Throwable> exceptions = new ArrayList<>();
TaskGroup pancakeFtg = pancakeF.taskGroup();
TaskGroup.InvocationContext context = pancakeFtg.newInvocationContext().withTerminateOnErrorStrategy(TaskGroupTerminateOnErrorStrategy.TERMINATE_ON_IN_PROGRESS_TASKS_COMPLETION);
IPancake rootPancake = pancakeFtg.invokeAsync(context).map(new Func1<Indexable, IPancake>() {
@Override
public IPancake call(Indexable indexable) {
IPancake pancake = (IPancake) indexable;
String name = pancake.name();
System.out.println("map.onNext:" + name);
seen.add(name);
return pancake;
}
}).onErrorResumeNext(new Func1<Throwable, Observable<IPancake>>() {
@Override
public Observable<IPancake> call(Throwable throwable) {
System.out.println("map.onErrorResumeNext:" + throwable);
exceptions.add(throwable);
return Observable.empty();
}
}).toBlocking().last();
Assert.assertTrue(Sets.difference(expectedToSee, seen).isEmpty());
Assert.assertEquals(exceptions.size(), 1);
Assert.assertTrue(exceptions.get(0) instanceof CompositeException);
CompositeException compositeException = (CompositeException) exceptions.get(0);
Assert.assertEquals(compositeException.getExceptions().size(), 2);
for (Throwable throwable : compositeException.getExceptions()) {
String message = throwable.getMessage();
Assert.assertTrue(message.equalsIgnoreCase("B") || message.equalsIgnoreCase("G"));
}
}
use of rx.exceptions.CompositeException in project azure-sdk-for-java by Azure.
the class ExternalChildResourceTests method shouldEmitErrorAfterAllSuccessfulCommit.
@Test
public void shouldEmitErrorAfterAllSuccessfulCommit() throws InterruptedException {
// Parent resource
ChickenImpl chicken = new ChickenImpl();
chicken.defineNewPullet("alice").withAge(1).withFailFlag(PulletImpl.FailFlag.OnCreate).attach().updatePullet("Clover").withAge(2).parent().updatePullet("Goldilocks").withAge(2).withFailFlag(PulletImpl.FailFlag.OnUpdate).parent().withoutPullet("Pinky");
final List<PulletImpl> changedPuppets = new ArrayList<>();
final List<Throwable> throwables = new ArrayList<>();
final CountDownLatch monitor = new CountDownLatch(1);
PulletsImpl pullets = chicken.pullets();
pullets.commitAsync().subscribe(new Observer<PulletImpl>() {
@Override
public void onCompleted() {
monitor.countDown();
Assert.assertTrue("onCompleted should not be invoked", false);
}
@Override
public void onError(Throwable throwable) {
try {
CompositeException exception = (CompositeException) throwable;
Assert.assertNotNull(exception);
for (Throwable innerThrowable : exception.getExceptions()) {
throwables.add(innerThrowable);
}
} finally {
monitor.countDown();
}
}
@Override
public void onNext(PulletImpl pullet) {
changedPuppets.add(pullet);
}
});
monitor.await();
Assert.assertTrue(throwables.size() == 2);
Assert.assertTrue(changedPuppets.size() == 2);
}
Aggregations