use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.
the class ExceptionHelper method addThrowable.
public static <T> boolean addThrowable(AtomicReference<Throwable> field, Throwable exception) {
for (; ; ) {
Throwable current = field.get();
if (current == TERMINATED) {
return false;
}
Throwable update;
if (current == null) {
update = exception;
} else {
update = new CompositeException(current, exception);
}
if (field.compareAndSet(current, update)) {
return true;
}
}
}
use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.
the class ExceptionHelper method flatten.
/**
* Returns a flattened list of Throwables from tree-like CompositeException chain.
* @param t the starting throwable
* @return the list of Throwables flattened in a depth-first manner
*/
public static List<Throwable> flatten(Throwable t) {
List<Throwable> list = new ArrayList<Throwable>();
ArrayDeque<Throwable> deque = new ArrayDeque<Throwable>();
deque.offer(t);
while (!deque.isEmpty()) {
Throwable e = deque.removeFirst();
if (e instanceof CompositeException) {
CompositeException ce = (CompositeException) e;
List<Throwable> exceptions = ce.getExceptions();
for (int i = exceptions.size() - 1; i >= 0; i--) {
deque.offerFirst(exceptions.get(i));
}
} else {
list.add(e);
}
}
return list;
}
use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.
the class BaseTestConsumer method fail.
/**
* Fail with the given message and add the sequence of errors as suppressed ones.
* <p>Note this is deliberately the only fail method. Most of the times an assertion
* would fail but it is possible it was due to an exception somewhere. This construct
* will capture those potential errors and report it along with the original failure.
*
* @param message the message to use
* @return AssertionError the prepared AssertionError instance
*/
protected final AssertionError fail(String message) {
StringBuilder b = new StringBuilder(64 + message.length());
b.append(message);
b.append(" (").append("latch = ").append(done.getCount()).append(", ").append("values = ").append(values.size()).append(", ").append("errors = ").append(errors.size()).append(", ").append("completions = ").append(completions);
if (timeout) {
b.append(", timeout!");
}
if (isDisposed()) {
b.append(", disposed!");
}
CharSequence tag = this.tag;
if (tag != null) {
b.append(", tag = ").append(tag);
}
b.append(')');
AssertionError ae = new AssertionError(b.toString());
if (!errors.isEmpty()) {
if (errors.size() == 1) {
ae.initCause(errors.get(0));
} else {
CompositeException ce = new CompositeException(errors);
ae.initCause(ce);
}
}
return ae;
}
use of io.reactivex.exceptions.CompositeException in project RxJava by ReactiveX.
the class CompositeDisposableTest method testCompositeException.
@Test
public void testCompositeException() {
final AtomicInteger counter = new AtomicInteger();
CompositeDisposable s = new CompositeDisposable();
s.add(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
throw new RuntimeException("failed on first one");
}
}));
s.add(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
throw new RuntimeException("failed on second one too");
}
}));
s.add(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
}
}));
try {
s.dispose();
fail("Expecting an exception");
} catch (CompositeException e) {
// we expect this
assertEquals(e.getExceptions().size(), 2);
}
// we should still have disposed to the second one
assertEquals(1, counter.get());
}
use of io.reactivex.exceptions.CompositeException in project retrofit by square.
the class ObservableThrowingTest method responseThrowingInOnErrorDeliveredToPlugin.
@Test
public void responseThrowingInOnErrorDeliveredToPlugin() {
server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AFTER_REQUEST));
final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (!throwableRef.compareAndSet(null, throwable)) {
throw Exceptions.propagate(throwable);
}
}
});
RecordingObserver<Response<String>> observer = subscriberRule.create();
final AtomicReference<Throwable> errorRef = new AtomicReference<>();
final RuntimeException e = new RuntimeException();
service.response().subscribe(new ForwardingObserver<Response<String>>(observer) {
@Override
public void onError(Throwable throwable) {
if (!errorRef.compareAndSet(null, throwable)) {
throw Exceptions.propagate(throwable);
}
throw e;
}
});
// noinspection ThrowableResultOfMethodCallIgnored
CompositeException composite = (CompositeException) throwableRef.get();
assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Aggregations