Solve the problem of io.reactivex.exceptions.UndeliverableException when using RxJava

When using RxJava, sometimes there will be: io.reactivex.exceptions.UndeliverableException.
The reason for this exception is probably: onError has been called multiple times. Normally, the first onError will be handled by the normal Observer, and the rest will be handled by the ErrorHandler. You can capture multiple errors by the following method. This method only needs to be executed once in Application.

private void setRxJavaErrorHandler() {
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
        @Override
        public void accept(Throwable e) {
            if (e instanceof UndeliverableException) {
                e = e.getCause();
                Log.d(TAG, "UndeliverableException=" + e);
                return;
            } else if ((e instanceof IOException)) {
                // fine, irrelevant network problem or API that throws on cancellation
                return;
            } else if (e instanceof InterruptedException) {
                // fine, some blocking code was interrupted by a dispose call
                return;
            } else if ((e instanceof NullPointerException) || (e instanceof IllegalArgumentException)) {
                // that's likely a bug in the application
                Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
                return;
            } else if (e instanceof IllegalStateException) {
                // that's a bug in RxJava or in a custom operator
                Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
                return;
            }
            Log.d(TAG, "unknown exception=" + e);
        }
    });
}

 

Guess you like

Origin blog.csdn.net/chenzhengfeng/article/details/106949562