如何在 RxJava 地图运算符中使应用程序崩溃

How to crash app inside RxJava map operator

例如,当调用 RxJava 映射运算符后出现 NullPointerException 时,应用程序不会崩溃。我希望应用程序在发生这种情况时崩溃,以便它可以向 crashlytics 等提交报告。

我尝试在 try/catch 块中使用 Exceptions.propagate(),但没有用。

我找到的唯一解决方案是在我的错误处理程序中抛出 RuntimeException

    override fun getCategories(): Single<List<Category>> {
        return ApiManager
                .getCategoriesService()
                .getCategories()
                .map { categoriesResponse ->
                        throw KotlinNullPointerException
                        categoriesResponse.categories?.map { categoryResponse ->
                            CategoryMapper().mapFromRemote(categoryResponse)
                        }
                }
    }

地图运算符内部抛出的 NullPointerException 不会使应用程序崩溃。 如果我在 return 语句之前调用它,它会使应用程序崩溃。

If I call it before the return statement it crashes the app.

它崩溃了,因为 getCategories 方法是 运行 在 Android 的主线程上构建 rx 链。

The NullPointerException thrown inside the map operator does not crash the app.

它不会使应用程序崩溃,因为此链订阅的线程不是 Android 的主线程。例如。你的连锁店有 .subscribeOn(Schedulers.io()).

The only solution that I found was throwing a RuntimeException in my error handler.

这是您链条的预期设计 document:

An Observable typically does not throw exceptions. Instead it notifies any observers that an unrecoverable error has occurred by terminating the Observable sequence with an onError notification.

So rather than catch exceptions, your observer or operator should more typically respond to onError notifications of exceptions.

好的,所以我认为最接近我想要实现的是通过在 onError 中调用的错误处理程序中调用 Exceptions.propagate(throwable)。 感谢您为我指明正确的方向 @Gustavo @TooManyEduardos2