RxJava flatmap: how to check which observable emitted an error

I'm using RxJava's flatmap in order to execute multiple calls in sequence where one call relys on the previous call. I also need to know which observable emitted an error in the case that onError is called in order to properly implement my error handling. How do I achieve this?

Here is my code:

mSubscription = RxUtil.callObservable(mDataManager.createAccount(email, password))
            .flatMap(new Func1<AuthResult, Observable<Void>>() {
                @Override
                public Observable<Void> call(AuthResult authResult) {

                    User user = new User(0, null, null, name, null, username, 0, 0);

                    return RxUtil.callObservable(mDataManager.createUser(authResult.getUser().getUid(), user));
                }
            })
            .subscribe(new Subscriber<Void>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable throwable) {
                    try {
                        throw (Exception) throwable;
                    } catch (FirebaseAuthUserCollisionException ucException) {
                        getPickUpView().showError(PickUpApplication.getContext().getString(R.string.error_account_exists));
                    } catch (Exception exception) {
                        getPickUpView().showError(PickUpApplication.getContext().getString(R.string.error_account_general));
                    }
                }

                @Override
                public void onNext(Void aVoid) {
                    getPickUpView().createAccountSuccessful(authResult);
                }
            });

1 answer

  • answered 2018-02-13 01:46 Jacob Stinson

    I was thinking about this the wrong way. Is summary, I thought this was an issue that I needed to address when i didn't. RxJava will emit all errors in the onError method no matter what observable emits the error. Once onError is called the subscription is done, so the flatmap call will never take place.

    In summary, all I need to do is handle my errors from both observables I call (the original and the one in the flatmap) in the same onError method.