Search in sources :

Example 1 with CompositeException

use of io.reactivex.exceptions.CompositeException in project vertx-examples by vert-x3.

the class Transaction method start.

@Override
public void start() throws Exception {
    JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
    String sql = "CREATE TABLE colors (" + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " + "name VARCHAR(255), " + "datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
    JDBCClient client = JDBCClient.createShared(vertx, config);
    // Connect to the database
    client.rxGetConnection().flatMap(conn -> conn.rxSetAutoCommit(false).toSingleDefault(false).flatMap(autoCommit -> conn.rxExecute(sql).toSingleDefault(true)).flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE"))).flatMap(updateResult -> conn.rxCommit().toSingleDefault(true).map(commit -> updateResult)).onErrorResumeNext(ex -> conn.rxRollback().toSingleDefault(true).onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2))).flatMap(ignore -> Single.error(ex))).flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors")).doAfterTerminate(conn::close)).subscribe(resultSet -> {
        // Subscribe to get the final result
        System.out.println("Results : " + resultSet.getRows());
    }, Throwable::printStackTrace);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) CompositeException(io.reactivex.exceptions.CompositeException) JDBCClient(io.vertx.reactivex.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Single(io.reactivex.Single) Runner(io.vertx.example.util.Runner) JsonArray(io.vertx.core.json.JsonArray) CompositeException(io.reactivex.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.reactivex.ext.jdbc.JDBCClient)

Example 2 with CompositeException

use of io.reactivex.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.setErrorHandler(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            if (!pluginRef.compareAndSet(null, throwable)) {
                // Don't swallow secondary errors!
                throw Exceptions.propagate(throwable);
            }
            latch.countDown();
        }
    });
    TestObserver<Void> observer = new TestObserver<>();
    final RuntimeException e = new RuntimeException();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    service.completable().subscribe(new ForwardingCompletableObserver(observer) {

        @Override
        public void onError(Throwable throwable) {
            errorRef.set(throwable);
            throw e;
        }
    });
    latch.await(1, SECONDS);
    // noinspection ThrowableResultOfMethodCallIgnored
    CompositeException composite = (CompositeException) pluginRef.get();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CompositeException(io.reactivex.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeException(io.reactivex.exceptions.CompositeException) IOException(java.io.IOException) TestObserver(io.reactivex.observers.TestObserver) ForwardingCompletableObserver(retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver) Test(org.junit.Test)

Example 3 with CompositeException

use of io.reactivex.exceptions.CompositeException in project retrofit by square.

the class ObservableThrowingTest method bodyThrowingInOnErrorDeliveredToPlugin.

@Test
public void bodyThrowingInOnErrorDeliveredToPlugin() {
    server.enqueue(new MockResponse().setResponseCode(404));
    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<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;
        }
    });
    // noinspection ThrowableResultOfMethodCallIgnored
    CompositeException composite = (CompositeException) throwableRef.get();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CompositeException(io.reactivex.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompositeException(io.reactivex.exceptions.CompositeException) Test(org.junit.Test)

Example 4 with CompositeException

use of io.reactivex.exceptions.CompositeException in project retrofit by square.

the class ObservableThrowingTest method resultThrowingInOnErrorDeliveredToPlugin.

@Test
public void resultThrowingInOnErrorDeliveredToPlugin() {
    server.enqueue(new MockResponse());
    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<Result<String>> observer = subscriberRule.create();
    final RuntimeException first = new RuntimeException();
    final RuntimeException second = new RuntimeException();
    service.result().subscribe(new ForwardingObserver<Result<String>>(observer) {

        @Override
        public void onNext(Result<String> value) {
            // The only way to trigger onError for a result is if onNext throws.
            throw first;
        }

        @Override
        public void onError(Throwable throwable) {
            throw second;
        }
    });
    // noinspection ThrowableResultOfMethodCallIgnored
    CompositeException composite = (CompositeException) throwableRef.get();
    assertThat(composite.getExceptions()).containsExactly(first, second);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CompositeException(io.reactivex.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompositeException(io.reactivex.exceptions.CompositeException) Test(org.junit.Test)

Example 5 with CompositeException

use of io.reactivex.exceptions.CompositeException in project retrofit by square.

the class FlowableThrowingTest 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);
            }
        }
    });
    RecordingSubscriber<Response<String>> subscriber = subscriberRule.create();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final RuntimeException e = new RuntimeException();
    service.response().subscribe(new ForwardingSubscriber<Response<String>>(subscriber) {

        @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);
}
Also used : Response(retrofit2.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) CompositeException(io.reactivex.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompositeException(io.reactivex.exceptions.CompositeException) Test(org.junit.Test)

Aggregations

CompositeException (io.reactivex.exceptions.CompositeException)21 Test (org.junit.Test)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 MockResponse (okhttp3.mockwebserver.MockResponse)14 Response (retrofit2.Response)4 Ignore (org.junit.Ignore)2 ParseException (android.net.ParseException)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSerializer (com.google.gson.JsonSerializer)1 Single (io.reactivex.Single)1 TestObserver (io.reactivex.observers.TestObserver)1 RxCacheException (io.rx_cache2.RxCacheException)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 Runner (io.vertx.example.util.Runner)1 AbstractVerticle (io.vertx.reactivex.core.AbstractVerticle)1 JDBCClient (io.vertx.reactivex.ext.jdbc.JDBCClient)1 IOException (java.io.IOException)1 NotSerializableException (java.io.NotSerializableException)1 ConnectException (java.net.ConnectException)1