Skip to content

Instantly share code, notes, and snippets.

@Tagakov
Last active August 29, 2015 14:21
Show Gist options
  • Save Tagakov/6cabd31d32304788169b to your computer and use it in GitHub Desktop.
Save Tagakov/6cabd31d32304788169b to your computer and use it in GitHub Desktop.
Stack overflow question about rxjava and retrofit. How to transform observable's error to another observable?
How do I transform observable's error to another observable?
I am trying to implement algorithm showed on scheme below:
![Block scheme][1]
[1]: http://i.stack.imgur.com/777p5.png
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment