How do I transform observable's error to another observable?
I am trying to implement algorithm showed on scheme below:
I am using a tutorial named Grokking RxJava to start my learning and found that flatMap operator can convert one Observable returning by Retrofit into another which allows me to do a chain of invocations of server methods. But in the tutorial chaining methods always return success result. How I can do the same chaining if one of the remote methods returns error?
Currently I'm using an approach that looks odd and unclear to me:
AppObservable.bindActivity(this, userService.checklogin(mPhone)
.onErrorResumeNext(new Func1<Throwable, Observable<? extends Response>>() {
@Override
public Observable<? extends Response> call(Throwable throwable) {
return Observable.just(null);
}
}))
.flatMap(new Func1<Response, Observable<Response>>() {
@Override
public Observable<Response> call(Response response) {
if (response == null) {
return AppObservable.bindActivity(RegistrationActivity.this, userService.register(
mPhone,
name.getText().toString(),
selectedSex,
selectedDateDay,
selectedDateMonth,
selectedDateYear,
Locale.getDefault().getLanguage(),
persistentUserInfoStore.getInstallationToken()
));
}
phone.setError(getString(R.string.already_registered_phone));
progressDialog.dismiss();
return Observable.empty();
}
})
.subscribe(new Action1<Response>() {
@Override
public void call(Response response) {
startConfirmationActivity();
progressDialog.dismiss();
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
progressDialog.dismiss();
Toast.makeText(RegistrationActivity.this, "Error while register user", Toast.LENGTH_SHORT).show();
}
});
Is there any more clear approach to do this.
P.S. I am very new in RxJava.
userService.register
fromonErrorResumeNext
function? – Vladimir Mironov