I am using Retrofit to access my API as follows:
public interface UserService {
...
@POST("/user/login")
public Observable<User> register(@Body() User user);
}
Here is how I access my API:
mUserService.register(user)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<User>() {
@Override
public void onCompleted() {
....
}
@Override
public void onError(Throwable e) {
....
}
@Override
public void onNext(User user) {
....
}
});
This works perfectly well, except when there is an exception (i.e. IOException, or when connection times out), the onError method doesn't get fired, instead I get an exception on the main thread which terminates my application.
However, for some cases (such as when the API call responds with status code 400), the onError method is fired as expected.
After digging the logcat output, I have spotted this line, (not sure how I am supposed to deal with this)
rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
Can someone let me know where I am doing things wrong?
In my previous experience with RxJava the OnErrorFailedException
means that an exception occurred while handling another exception in the onError
method.
If you don't see stack trace of the real cause it's probably due the bug in RxJava with CompositeException
(versions 0.19.2 and above) https://github.com/Netflix/RxJava/issues/1405
As a workaround try to wrap your onError
code within try-catch
block and log the exception. This way you will see what's the problem with your onError
implementation and you will be able to solve the problem.
@Override
public void onError(Throwable e) {
try {
...
catch(Throwable e) {
// Log the exception
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With