解決使用RxJava時出現的io.reactivex.exceptions.UndeliverableException異常問題

使用RxJava時,有時會出現:io.reactivex.exceptions.UndeliverableException異常。
出現這個異常的原因大概是:調用了多次onError。正常來說第一次onError會走正常Observer處理,其他的會走ErrorHandler。可以通過如下方法捕捉多次的error,此方法只需要在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);
        }
    });
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章