RxJava UndeliverableException how to handle onSuccess consumer exception?

eri*_*icn 2 kotlin rx-java rx-java2

The exception ended up not handled and was reported as a non-fatal in Crashlytics, hence fell off our radar User saw a blank screen which is not desirable A crash would have been better, for fail-fast is preferred

Ideally, I'd like to the onError consumer to be trigger, that's where I handle the error e.g. shows error UI for every stream

However,

fun main() {
    Single.just(listOf("efaewf"))
            .subscribe({
                println("result is ${it[1]}")
            }, {
                println("handling exception")
                it.printStackTrace()
            })
}
Run Code Online (Sandbox Code Playgroud)

shows that onError consumer is not working

io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:65)
    at io.reactivex.internal.operators.single.SingleJust.subscribeActual(SingleJust.java:30)
    at io.reactivex.Single.subscribe(Single.java:3603)
    at io.reactivex.Single.subscribe(Single.java:3589)
    at com.coffeemeetsbagel.DummyKt.main(Dummy.kt:7)
    at com.coffeemeetsbagel.DummyKt.main(Dummy.kt)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.Collections$SingletonList.get(Collections.java:4817)
    at com.coffeemeetsbagel.DummyKt$main$1.accept(Dummy.kt:8)
    at com.coffeemeetsbagel.DummyKt$main$1.accept(Dummy.kt)
    at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:62)
    ... 5 more
Exception in thread "main" io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:65)
    at io.reactivex.internal.operators.single.SingleJust.subscribeActual(SingleJust.java:30)
    at io.reactivex.Single.subscribe(Single.java:3603)
    at io.reactivex.Single.subscribe(Single.java:3589)
    at com.coffeemeetsbagel.DummyKt.main(Dummy.kt:7)
    at com.coffeemeetsbagel.DummyKt.main(Dummy.kt)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.Collections$SingletonList.get(Collections.java:4817)
    at com.coffeemeetsbagel.DummyKt$main$1.accept(Dummy.kt:8)
    at com.coffeemeetsbagel.DummyKt$main$1.accept(Dummy.kt)
    at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:62)
    ... 5 more
Run Code Online (Sandbox Code Playgroud)

I've read the docs here but don't comprehend fully

I could totally add if {} else {} or try {} catch {} to onSuccess consumer but it feels like code smell

我错了吗? if else/ try catch 是处理异常的最佳和/或预期方法吗onSuccess

aka*_*okd 5

您正在使用Single哪个有协议onSuccess | onError。因此,如果onSuccess崩溃,它无法onError在同一个观察者上调用。

相反,Observable有协议onNext* (onError|onComplete)?,因此允许崩溃onNext调用onError

您有以下几种选择Single

  • 将其转换为Observable订阅之前,
  • 在不可靠处理程序中添加 try-catch onSuccess
  • 在早期的运算符(例如 )中进行映射/转换,map因此最终消费者不太可能崩溃。