Search in sources :

Example 1 with RxJavaErrorHandler

use of rx.plugins.RxJavaErrorHandler in project realm-java by realm.

the class NewsReaderApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    context = this;
    initializeTimber();
    RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {

        @Override
        public void handleError(Throwable e) {
            super.handleError(e);
            Timber.e(e.toString());
        }
    });
    // Configure default configuration for Realm
    Realm.init(this);
    RealmConfiguration realmConfig = new RealmConfiguration.Builder().build();
    Realm.setDefaultConfiguration(realmConfig);
}
Also used : RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) RealmConfiguration(io.realm.RealmConfiguration)

Example 2 with RxJavaErrorHandler

use of rx.plugins.RxJavaErrorHandler in project retrofit by square.

the class AsyncTest method throwingInOnCompleteDeliveredToPlugin.

@Test
public void throwingInOnCompleteDeliveredToPlugin() throws InterruptedException {
    server.enqueue(new MockResponse());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {

        @Override
        public void handleError(Throwable throwable) {
            if (!errorRef.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();
    service.completable().unsafeSubscribe(new AsyncCompletableSubscriber() {

        @Override
        public void onCompleted() {
            throw e;
        }

        @Override
        public void onError(Throwable t) {
            subscriber.onError(t);
        }
    });
    latch.await(1, SECONDS);
    assertThat(errorRef.get()).isSameAs(e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) AsyncCompletableSubscriber(rx.observers.AsyncCompletableSubscriber) TestSubscriber(rx.observers.TestSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with RxJavaErrorHandler

use of rx.plugins.RxJavaErrorHandler 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;
        }
    });
    latch.await(1, SECONDS);
    //noinspection ThrowableResultOfMethodCallIgnored
    CompositeException composite = (CompositeException) pluginRef.get();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) AsyncCompletableSubscriber(rx.observers.AsyncCompletableSubscriber) CompositeException(rx.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 4 with RxJavaErrorHandler

use of rx.plugins.RxJavaErrorHandler in project ratpack by ratpack.

the class RxRatpack method initialize.

/**
   * Registers an {@link RxJavaObservableExecutionHook} with RxJava that provides a default error handling strategy of forwarding exceptions to the execution error handler.
   * <p>
   * This method is idempotent.
   * It only needs to be called once per JVM, regardless of how many Ratpack applications are running within the JVM.
   * <p>
   * For a Java application, a convenient place to call this is in the handler factory implementation.
   * <pre class="java">{@code
   * import ratpack.error.ServerErrorHandler;
   * import ratpack.rx.RxRatpack;
   * import ratpack.test.embed.EmbeddedApp;
   * import rx.Observable;
   * import static org.junit.Assert.assertEquals;
   *
   * public class Example {
   *   public static void main(String... args) throws Exception {
   *     RxRatpack.initialize(); // must be called once for the life of the JVM
   *
   *     EmbeddedApp.fromHandlers(chain -> chain
   *       .register(s -> s
   *         .add(ServerErrorHandler.class, (ctx, throwable) ->
   *           ctx.render("caught by error handler: " + throwable.getMessage())
   *         )
   *       )
   *       .get(ctx -> Observable.<String>error(new Exception("!")).subscribe(ctx::render))
   *     ).test(httpClient ->
   *       assertEquals("caught by error handler: !", httpClient.getText())
   *     );
   *   }
   * }
   * }</pre>
   */
@SuppressWarnings("deprecation")
public static void initialize() {
    RxJavaPlugins plugins = RxJavaPlugins.getInstance();
    ExecutionHook ourHook = new ExecutionHook();
    try {
        plugins.registerObservableExecutionHook(ourHook);
    } catch (IllegalStateException e) {
        RxJavaObservableExecutionHook existingHook = plugins.getObservableExecutionHook();
        if (!(existingHook instanceof ExecutionHook)) {
            throw new IllegalStateException("Cannot install RxJava integration because another execution hook (" + existingHook.getClass() + ") is already installed");
        }
    }
    ErrorHandler ourErrorHandler = new ErrorHandler();
    try {
        plugins.registerErrorHandler(ourErrorHandler);
    } catch (IllegalStateException e) {
        RxJavaErrorHandler existingErrorHandler = plugins.getErrorHandler();
        if (!(existingErrorHandler instanceof ErrorHandler)) {
            throw new IllegalStateException("Cannot install RxJava integration because another error handler (" + existingErrorHandler.getClass() + ") is already installed");
        }
    }
    try {
        plugins.registerSchedulersHook(new DefaultSchedulers());
    } catch (IllegalStateException e) {
        rx.plugins.RxJavaSchedulersHook existingSchedulers = plugins.getSchedulersHook();
        if (!(existingSchedulers instanceof DefaultSchedulers)) {
            throw new IllegalStateException("Cannot install RxJava integration because another set of default schedulers (" + existingSchedulers.getClass() + ") is already installed");
        }
    }
}
Also used : RxJavaPlugins(rx.plugins.RxJavaPlugins) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) RxJavaObservableExecutionHook(rx.plugins.RxJavaObservableExecutionHook) RxJavaObservableExecutionHook(rx.plugins.RxJavaObservableExecutionHook) DefaultSchedulers(ratpack.rx.internal.DefaultSchedulers)

Aggregations

RxJavaErrorHandler (rx.plugins.RxJavaErrorHandler)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.junit.Test)2 AsyncCompletableSubscriber (rx.observers.AsyncCompletableSubscriber)2 TestSubscriber (rx.observers.TestSubscriber)2 RealmConfiguration (io.realm.RealmConfiguration)1 DefaultSchedulers (ratpack.rx.internal.DefaultSchedulers)1 CompositeException (rx.exceptions.CompositeException)1 RxJavaObservableExecutionHook (rx.plugins.RxJavaObservableExecutionHook)1 RxJavaPlugins (rx.plugins.RxJavaPlugins)1