Mapping exception in RxJava 2

前提是你 提交于 2019-12-02 06:08:00

问题


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!


回答1:


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);
}


来源:https://stackoverflow.com/questions/45885416/mapping-exception-in-rxjava-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!