2017-10-13 66 views
0

捕獲我正在向使用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)的代碼仍然執行。

回答

1

CompositeException表示實際問題在handleRegistrationError(error)之內。不知怎的,你這樣做導致

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

最有可能它是如此的只是實現這個(或刪除TODO())與你實現TODO()

fun TODO(): Nothing = throw NotImplementedError() 

功能的東西,你可能會解決您的問題。

+0

你說得對。我是Kotlin的新手,不知道TODO可能會拋出NotImplementedError。感謝您的回答。標記爲已接受! –