kotlin - RxJava UndeliverableException如何处理onSuccess消费者异常?
问题描述
异常最终没有被处理,并且在 Crashlytics 中被报告为非致命的,因此从我们的雷达中消失了用户看到了一个不可取的空白屏幕崩溃会更好,因为首选快速失败
理想情况下,我希望onError
消费者被触发,这就是我处理错误的地方,例如显示每个流的错误 UI
然而,
fun main() {
Single.just(listOf("efaewf"))
.subscribe({
println("result is ${it[1]}")
}, {
println("handling exception")
it.printStackTrace()
})
}
表明onError
消费者没有工作
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
我已经阅读了这里的文档,但没有完全理解
我可以完全添加if {} else {}
或添加try {} catch {}
到onSuccess
消费者,但感觉就像代码气味
我错了吗,如果 else/try 捕捉到处理异常的最佳和/或预期方法onSuccess
吗?
解决方案
您正在使用Single
哪个具有协议onSuccess | onError
。所以如果onSuccess
崩溃,它不能onError
在同一个观察者上调用。
相反,Observable
有协议,因此允许调用onNext* (onError|onComplete)?
崩溃。onNext
onError
你有几个选择Single
:
- 将其转换为
Observable
订阅前, onSuccess
在不可靠的处理程序中添加 try-catch ,- 在较早的运算符中进行映射/转换,例如
map
,因此最终消费者不太可能崩溃。
推荐阅读
- batch-file - 如何通过批处理文件将驱动器映射为系统帐户
- c# - 流上需要一个进程中的“管道”
- php - Laravel 任务 - 存储上次运行的时间
- updates - MarkLogic 集群 - 在第一台主机中添加数据并在第二台主机中更新会引发错误
- javascript - 上传图片并停留在页面上
- scala - Scala Play:使用正则表达式路由可选参数?
- android - ReactAndroid:react-native升级时buildReadctNdkLib FAILED
- javascript - 通过 HTML 表单页面安全地将 JSON 发送到服务器
- elasticsearch - Percentage for each bucket
- python - 在抓取 Web 内容时,无法使用先例函数输出作为下一个函数的输入