HttpException未被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() ),你可能会解决你的问题。