HttpException 未被 onError() 捕获

HttpException not caught by onError()

我正在使用 Retrofit2 和 RxJava2 向后端服务器发出请求。当响应为 200 或 201 时,一切正常。当服务器的响应是 409 或 503 并且抛出 HttpException 时,它不会被 Observable 的 onError() 捕获并且应用程序崩溃。

我提出的要求是这样的:

@POST("users/sign-up")
fun register(@Body register: RegisterBody): Observable<User>

我提出请求的代码片段是这样的(applySchedulers() 仅适用于 subscribeOn()observeOn()):

api.register(body)
   .compose(applySchedulers())
   .subscribe(
        { user -> onNextRegister(user.id, email.toString(), password.toString()) },
        { error -> handleRegistrationError(error) })

抛出的异常如下:

 io.reactivex.exceptions.CompositeException: 2 exceptions occurred.
 ComposedException 1 : retrofit2.adapter.rxjava2.HttpException: HTTP 503 Unavailable
 ComposedException 2 : kotlin.NotImplementedError: An operation is not implemented: not implemented

即使我为 Observable 实现了 onError(),如何防止应用程序崩溃?请注意,handleRegistrationError(error) 中的代码仍在执行。

CompositeException 表示实际问题在 handleRegistrationError(error) 内。不知何故你做了一些导致

kotlin.NotImplementedError: An operation is not implemented: not implemented

很可能与您实现的功能有关 TODO()

fun TODO(): Nothing = throw NotImplementedError()

因此只需执行此操作(或删除 TODO()),您可能会解决您的问题。