question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple errors in zip'ed observables with flatMap

See original GitHub issue

While reading Error handling on wiki I realized that in a zip operator with a multiple network calls, it’s possible to receive a crash with an UndeliverableException. I tried to reproduce this behavior:

RxJava version: 2.2.0

Reproduce code:

    @Test
    public void testUncaughtException() throws InterruptedException {
        Observable first = Observable.create(e -> {
            System.out.println("first");
            throw new HelperException("first exception");
        });

        Observable second = Observable.create(e -> {
            System.out.println("second");
            throw new HelperException("second exception");
        });

        List<Observable<?>> observableList = new ArrayList<>();
        observableList.add(first);
        observableList.add(second);

        Observable.zip(observableList, objects -> "result")
                .subscribeOn(Schedulers.io())
                .subscribe(
                        System.out::println,
                        t -> {
                            System.out.println("exception caught!");
                        }
                );
        Thread.sleep(2000);
    }

The output as expected:

first
exception caught!
second
io.reactivex.exceptions.UndeliverableException: ... HelperException: second exception ...

And the second test:

    @Test
    public void testUncaughtExceptionWithFlatMap() throws InterruptedException {
        Observable testObservable = Observable.create(e -> e.onNext(""))
                .flatMap((Function<Object, ObservableSource<?>>) o -> {

                    Observable first = Observable.create(e -> {
                        System.out.println("first");
                        throw new HelperException("first exception");
                    });

                    Observable second = Observable.create(e -> {
                        System.out.println("second");
                        throw new HelperException("second exception");
                    });

                    List<Observable<?>> observableList = new ArrayList<>();
                    observableList.add(first);
                    observableList.add(second);

                    return Observable.zip(observableList, objects -> "result");
                });

        testObservable
                .subscribeOn(Schedulers.io())
                .subscribe(
                        System.out::println,
                        t -> System.out.println("exception caught!")
                );

        Thread.sleep(2000);
    }

And I expected an UndeliverableException too, but the output is:

first
exception caught!

Is this behavior correct? Why there is no UndeliverableException in the second test?

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
akarnokdcommented, May 26, 2019

Correction. In both cases, you shouldn’t see any undeliverable exceptions. What happens is that flatMap issues a cancel when it detects the error which then stops the subscription to the second source. I’ll investigate this further.

0reactions
radityagumaycommented, Jun 23, 2019

@akarnokd thanks for explanations!

i am currently using onErrorResumeNext to tweak that part. so, whenever error occurs in observable i return default value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling for zipped observables - rx java - Stack Overflow
First of all, the right way to notify a Subscriber about an error is to call subscriber ... flatMap(permalink -> Observable.zip( Observable.
Read more >
RxSwift/RxCocoa : Handling errors with multiple observables ...
I´ve been trying to find a good, elegant way to handle errors when chaining multiple observables that can throw errors.
Read more >
RxJava — Handling Errors Like a Pro | by TC Wang - Medium
RxJava provides many different error handlers when an error is thrown inside of a stream. Two of the most basic but useful Observers...
Read more >
monix.reactive.Observable
Given the source observable and another Observable , emits all of the items from the first of these Observables to emit an item...
Read more >
4. Applying Reactive Programming to Existing Applications
We will spend much more time on error handling later in this book (see ... Remember that the zip() operator subscribes to two...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found