HttpException未被onError()
问题描述:
捕获我正在向使用Retrofit2和RxJava2的后端服务器发出请求。当答案是200或201时,一切正常。当服务器的响应为409或503并且引发HttpException
时,它不会被Observable的onError()
捕获,并且应用程序崩溃。HttpException未被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()
)与你实现TODO()
fun TODO(): Nothing = throw NotImplementedError()
功能的东西,你可能会解决您的问题。
你说得对。我是Kotlin的新手,不知道TODO可能会抛出NotImplementedError。感谢您的回答。标记为已接受! –