0

In the code below function f returns Single<String>,

   Observable.map { line ->
              f(line).doOnError { e ->
                    println("Error:$e")
                }
        }
        .subscribe({ record -> println(record) }, { e -> println("Error2:$e") })

println("Error:$e") inside the map won't execute, however I will be able to get the error printed in the subscriber. It looks like that the chaining inside the mapper function in not allowed. Is it correct? If yes, why?

Edit: Also tried flatmap, but same result.

   Observable.flatmap { line ->
              f(line).toObservable().doOnError { e ->
                    println("Error:$e")
                }
        }
        .subscribe({ record -> println(record) }, { e -> println("Error2:$e") })
Mangat Rai Modi
  • 5,397
  • 8
  • 45
  • 75
  • Did you forget [this](https://stackoverflow.com/a/49730841/61158)? – akarnokd Apr 10 '18 at 11:38
  • I do remember this, but I don't understand why would it be required. From here:- https://stackoverflow.com/questions/22847105/when-do-you-use-map-vs-flatmap-in-rxjava?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa I understood that flatmap takes a function which takes observable, but map can return anything. I tried flatmap also, but same result. See edit – Mangat Rai Modi Apr 10 '18 at 11:46
  • Although I do get `Observable` instead of `Observable`, which is good, but the problem of .doOnError remains. – Mangat Rai Modi Apr 10 '18 at 11:57
  • Thanks to @akarnokd my working example is wrong. I have isolated the problem somewhere else and asked the querstion here:- https://stackoverflow.com/questions/49756399/vertex-reactive-kafka-client-chaining-not-working-when-writing – Mangat Rai Modi Apr 10 '18 at 14:37

1 Answers1

1

This works as expected:

@Test
public void test() {
    Observable.just(1)
    .flatMap(v -> single(v)
            .toObservable()
            .doOnError(w -> System.out.println("Error2 " + w))
    )
    .subscribe(v -> System.out.println(v), e -> System.out.println("Error " + e));
}

Single<Integer> single(Integer v) {
    return Single.error(new IOException());
}

Prints:

Error2 java.io.IOException
Error java.io.IOException
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • Hmm.. I actually changed my code to abstract. Anyways Its a nice way to produce an error. I apologise as it looks I have abstracted away the root cause. I will post code shortly where I am able to reproduce the issue. – Mangat Rai Modi Apr 10 '18 at 12:16
  • That's why it is important you try the code you are about to post to check if it still exhibits the issue. If not, you can locally make out the diff and possibly discover the cause much quicker. – akarnokd Apr 10 '18 at 12:33
  • So I have isolated the problem in one of the libraries which wraps rxjava on their client. I believe I should be asking a separate for that. – Mangat Rai Modi Apr 10 '18 at 14:08