@Override public void onComplete(@NonNull Task<Void> task) { if (isDisposed()) return; if (!task.isSuccessful()) { Exception exception = task.getException(); if (terminated) { RxJavaPlugins.onError(exception); } else { try { terminated = true; observer.onError(exception); } catch (Throwable t) { Exceptions.throwIfFatal(t); RxJavaPlugins.onError(new CompositeException(task.getException(), t)); } } } }
@Override public void onResponse(Call<T> call, Response<T> response) { if (call.isCanceled()) return; try { observer.onNext(response); if (!call.isCanceled()) { terminated = true; observer.onComplete(); } } catch (Throwable t) { if (terminated) { RxJavaPlugins.onError(t); } else if (!call.isCanceled()) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } } }
@Override public void onResponse(Call<T> call, Response<T> response) { if (call.isCanceled()) return; try { observer.onNext( new ProgressBean<>(mTotal, mTotal, response) ); if (!call.isCanceled()) { terminated = true; observer.onComplete(); } } catch (Throwable t) { if (terminated) { RxJavaPlugins.onError(t); } else if (!call.isCanceled()) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } } }
@Override public void onError(Throwable throwable) { try { Result<R> error = Result.error(throwable); observer.onNext( new ProgressBean<>(-1, -1, error) ); } catch (Throwable t) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } return; } observer.onComplete(); }
@Override public void onNext(Response<R> response) { if (response.isSuccessful()) { if (response.body() != null) { observer.onNext(response.body()); } else { observer.onComplete(); } } else { terminated = true; Throwable t = new HttpException(response); try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } }
@Override public void onComplete(@NonNull Task<T> task) { if (isDisposed()) return; if (task.isSuccessful()) { T result = task.getResult(); if (result == null) { observer.onComplete(); } else { observer.onSuccess(result); } } else { try { observer.onError(task.getException()); } catch (Throwable t) { Exceptions.throwIfFatal(t); RxJavaPlugins.onError(new CompositeException(task.getException(), t)); } } }
/** * Try appending this FunctionTaggingException as the very last cause of * the given throwable. * @param ex the Throwable to append to * @return ex the original Throwable */ public Throwable appendLast(Throwable ex) { Throwable r = ex; Set<Throwable> memory = new HashSet<Throwable>(); while (ex.getCause() != null) { if (memory.add(ex)) { ex = ex.getCause(); } else { // didn't work RxJavaPlugins.onError(new CompositeException(r, this)); return r; } } try { ex.initCause(this); } catch (Throwable exc) { // didn't work, oh well RxJavaPlugins.onError(new CompositeException(r, this)); } return r; }
@Test public void onErrorCrash() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { SingleConsumers.subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() { @Override public void accept(Throwable t) throws Exception { throw new IOException(t); } }); processor.onError(new IllegalArgumentException()); assertTrue(events.toString(), events.isEmpty()); TestHelper.assertError(errors, 0, CompositeException.class); List<Throwable> inners = TestHelper.compositeList(errors.get(0)); TestHelper.assertError(inners, 0, IllegalArgumentException.class); TestHelper.assertError(inners, 1, IOException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void onErrorCrash() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { MaybeConsumers.subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() { @Override public void accept(Throwable t) throws Exception { throw new IOException(t); } }, this); processor.onError(new IllegalArgumentException()); assertTrue(events.toString(), events.isEmpty()); TestHelper.assertError(errors, 0, CompositeException.class); List<Throwable> inners = TestHelper.compositeList(errors.get(0)); TestHelper.assertError(inners, 0, IllegalArgumentException.class); TestHelper.assertError(inners, 1, IOException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void onErrorCrash() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { CompletableConsumers.subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() { @Override public void accept(Throwable t) throws Exception { throw new IOException(t); } }); processor.onError(new IllegalArgumentException()); assertTrue(events.toString(), events.isEmpty()); TestHelper.assertError(errors, 0, CompositeException.class); List<Throwable> inners = TestHelper.compositeList(errors.get(0)); TestHelper.assertError(inners, 0, IllegalArgumentException.class); TestHelper.assertError(inners, 1, IOException.class); } finally { RxJavaPlugins.reset(); } }
@SuppressWarnings("unchecked") @Test public void bothErrorDelayed() { Flowables.orderedMerge(Functions.<Integer>naturalComparator(), true, Flowable.<Integer>error(new IOException("first")), Flowable.<Integer>error(new IOException("second")) ) .test() .assertFailure(CompositeException.class) .assertOf(new Consumer<TestSubscriber<Integer>>() { @Override public void accept(TestSubscriber<Integer> ts) throws Exception { List<Throwable> list = TestHelper.compositeList(ts.errors().get(0)); TestHelper.assertError(list, 0, IOException.class, "first"); TestHelper.assertError(list, 1, IOException.class, "second"); } }); }
@SuppressWarnings("unchecked") @Test public void bothErrorDelayedBackpressured() { Flowables.orderedMerge(Functions.<Integer>naturalComparator(), true, Flowable.<Integer>error(new IOException("first")), Flowable.<Integer>error(new IOException("second")) ) .test(0L) .assertFailure(CompositeException.class) .assertOf(new Consumer<TestSubscriber<Integer>>() { @Override public void accept(TestSubscriber<Integer> ts) throws Exception { List<Throwable> list = TestHelper.compositeList(ts.errors().get(0)); TestHelper.assertError(list, 0, IOException.class, "first"); TestHelper.assertError(list, 1, IOException.class, "second"); } }); }
@SuppressWarnings("unchecked") @Test public void nonEmptyBothErrorDelayed() { Flowables.orderedMerge(Functions.<Integer>naturalComparator(), true, Flowable.just(1).concatWith(Flowable.<Integer>error(new IOException("first"))), Flowable.just(2).concatWith(Flowable.<Integer>error(new IOException("second"))) ) .test() .assertFailure(CompositeException.class, 1, 2) .assertOf(new Consumer<TestSubscriber<Integer>>() { @Override public void accept(TestSubscriber<Integer> ts) throws Exception { List<Throwable> list = TestHelper.compositeList(ts.errors().get(0)); TestHelper.assertError(list, 0, IOException.class, "first"); TestHelper.assertError(list, 1, IOException.class, "second"); } }); }
@SuppressWarnings("unchecked") @Test public void nonEmptyBothErrorDelayed2() { Flowables.orderedMerge(Functions.<Integer>naturalComparator(), true, Flowable.just(1).concatWith(Flowable.<Integer>error(new IOException("first"))), Flowable.just(2).concatWith(Flowable.<Integer>error(new IOException("second"))) ) .rebatchRequests(1) .test() .assertFailure(CompositeException.class, 1, 2) .assertOf(new Consumer<TestSubscriber<Integer>>() { @Override public void accept(TestSubscriber<Integer> ts) throws Exception { List<Throwable> list = TestHelper.compositeList(ts.errors().get(0)); TestHelper.assertError(list, 0, IOException.class, "first"); TestHelper.assertError(list, 1, IOException.class, "second"); } }); }
@Test public void usingDisposerThrows2() { Nono.using(Functions.justCallable(0), Functions.justFunction(ioError), new Consumer<Integer>() { @Override public void accept(Integer t) throws Exception { throw new IllegalArgumentException(); } } ) .test() .assertFailure(CompositeException.class) .assertOf(new Consumer<TestSubscriber<Void>>() { @SuppressWarnings("unchecked") @Override public void accept(TestSubscriber<Void> ts) throws Exception { TestHelper.assertCompositeExceptions(ts, IOException.class, IllegalArgumentException.class); } }); }
@Test public void doOnError3() { ioError .doOnError(new Consumer<Throwable>() { @Override public void accept(Throwable t) throws Exception { count++; throw new IllegalArgumentException(); } }) .test() .assertFailure(CompositeException.class) .assertOf(new Consumer<TestSubscriber<Void>>() { @Override public void accept(TestSubscriber<Void> ts) throws Exception { List<Throwable> list = TestHelper.compositeList(ts.errors().get(0)); TestHelper.assertError(list, 0, IOException.class); TestHelper.assertError(list, 1, IllegalArgumentException.class); } }); Assert.assertEquals(1, count); }
@Test public void retryPredicateThrows() { ioError .retry(new Predicate<Throwable>() { @Override public boolean test(Throwable e) throws Exception { throw new IllegalArgumentException(); } }) .test() .assertFailure(CompositeException.class) .assertOf(new Consumer<TestSubscriber<Void>>() { @Override public void accept(TestSubscriber<Void> ts) throws Exception { List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0)); TestHelper.assertError(errors, 0, IOException.class); TestHelper.assertError(errors, 1, IllegalArgumentException.class); } }); }
@Test public void blockingSubscribeErrorThrows() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { ioError .blockingSubscribe(this, new Consumer<Throwable>() { @Override public void accept(Throwable ex) throws Exception { throw new IllegalArgumentException(); } }); TestHelper.assertError(errors, 0, CompositeException.class); List<Throwable> ce = TestHelper.compositeList(errors.get(0)); TestHelper.assertError(ce, 0, IOException.class); TestHelper.assertError(ce, 1, IllegalArgumentException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void When_Build_Is_Debug_And_Exception_Is_Composite_Do_Not_Show_It() { when(exceptionFormatterUT.isBuildConfigDebug()).thenReturn(true); CompositeException exception = new CompositeException(new RuntimeException("1"), new NetworkResponse.NetworkException("2")); TestObserver<String> observer = exceptionFormatterUT.format(exception).test(); observer.awaitTerminalEvent(); observer.assertNoErrors(); observer.assertValueCount(1); assertEquals("2 exceptions occurred. " + System.getProperty(LINE_SEPARATOR) + "Chain of Causes for CompositeException In Order Received =>" + System.getProperty(LINE_SEPARATOR) + "RuntimeException -> 1" + System.getProperty(LINE_SEPARATOR) + "NetworkException -> 2" + System.getProperty(LINE_SEPARATOR), observer.values().get(0)); }
/** * Generic method to test migration failure when using an invalid migration script * * @param creator * creator implementation * @param dataProvider * data provider implementation * @param script * migration script * @throws Throwable */ protected void invalidMigrationScript(FieldSchemaCreator creator, DataProvider dataProvider, String script) throws Throwable { try (Tx tx = tx()) { try { if (getClass().isAnnotationPresent(MicroschemaTest.class)) { invalidMicroschemaMigrationScript(creator, dataProvider, script); } else { invalidSchemaMigrationScript(creator, dataProvider, script); } } catch (CompositeException e) { Throwable firstError = e.getExceptions().get(0); if (firstError instanceof javax.script.ScriptException) { throw firstError; } else { Throwable nestedError = firstError.getCause(); nestedError.printStackTrace(); throw nestedError; } } } }
@Override protected void subscribeActual(Observer<? super Response<T>> observer) { // Since Call is a one-shot type, clone it for each new observer. Call<T> call = originalCall.clone(); observer.onSubscribe(new CallDisposable(call)); boolean terminated = false; try { Response<T> response = call.execute(); if (!call.isCanceled()) { observer.onNext(response); } if (!call.isCanceled()) { terminated = true; observer.onComplete(); } } catch (Throwable t) { Exceptions.throwIfFatal(t); if (terminated) { RxJavaPlugins.onError(t); } else if (!call.isCanceled()) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } } }
@Override public void onError(Throwable throwable) { try { observer.onNext(Result.<R>error(throwable)); } catch (Throwable t) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } return; } observer.onComplete(); }
@Override public void onNext(Response<R> response) { if (response.isSuccessful()) { observer.onNext(response.body()); } else { terminated = true; Throwable t = new HttpException(response); try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } }
@Override public void onFailure(Call<T> call, Throwable t) { if (call.isCanceled()) return; try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } }
private void logError(Throwable throwable) { if (throwable instanceof CompositeException) { CompositeException realException = (CompositeException) throwable; List<Throwable> exceptions = realException.getExceptions(); for (Throwable each : exceptions) { log(each); } } else { log(throwable); } }
@Override public void onError(Response<T> response) { if (call.isCanceled()) return; Throwable throwable = response.getException(); try { terminated = true; observer.onError(throwable); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(throwable, inner)); } }
@Override protected void subscribeActual(Observer<? super ProgressBean<Response<T>>> observer) { // Since Call is a one-shot type, clone it for each new observer. Call<T> call = originalCall.clone(); Utils.replaceToProgressRequestBody(call, this); observer.onSubscribe(new CallDisposable(call)); mObserver = observer; boolean terminated = false; try { Response<T> response = call.execute(); if (!call.isCanceled()) { observer.onNext( new ProgressBean<>(mTotal, mTotal, response) ); } if (!call.isCanceled()) { terminated = true; observer.onComplete(); } } catch (Throwable t) { Exceptions.throwIfFatal(t); if (terminated) { RxJavaPlugins.onError(t); } else if (!call.isCanceled()) { try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } } }
@Override public void onNext(ProgressBean<Response<R>> progress) { Response<R> response = progress.data; if (response == null) { observer.onNext( new ProgressBean<R>(progress.total, progress.progress, null) ); } else { if (response.isSuccessful()) { observer.onNext( new ProgressBean<>(progress.total, progress.progress, response.body()) ); } else { terminated = true; Throwable t = new HttpException(response); try { observer.onError(t); } catch (Throwable inner) { Exceptions.throwIfFatal(inner); RxJavaPlugins.onError(new CompositeException(t, inner)); } } } }