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);
}
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);
}
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);
}
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");
}
}
}
Aggregations