piotrpawlowski
piotrpawlowski

Reputation: 786

Mapping exception in RxJava 2

How can I map one occurred exception to another one in RxJava2? For example:

doOnError(throwable -> {
    if (throwable instanceof FooException) {
        throw new BarException();
    }
})

In this case I finally receive CompositeException that consists of FooException and BarException, but I'd like to receive only BarException. Help!

Upvotes: 8

Views: 5068

Answers (1)

akarnokd
akarnokd

Reputation: 69997

You can use onErrorResumeNext and return Observable.error() from it:

source.onErrorResumeNext(e -> Observable.error(new BarException()))

Edit

This test passes for me:

@Test
public void test() {
    Observable.error(new IOException())
    .onErrorResumeNext((Throwable e) -> Observable.error(new IllegalArgumentException()))
    .test()
    .assertFailure(IllegalArgumentException.class);
}

Upvotes: 17

Related Questions