1.x Single#flatMapCompletable calls hooks onError when mapping to an error completeable
If you flatmap a Single into a Completeable, and the Completeable is an error, you will have the exception passed into the RxJavaHooks.onError global catch function as well as ending up with the Completeable Error you expect to see.
I have managed to get is down to the follow test case highlighting the problem,
@Test
public void flatMapCompletableErrorCallsHooksErrorHandler() {
AtomicBoolean wasHooksHandlerCalled = new AtomicBoolean();
RxJavaHooks.setOnError(t -> wasHooksHandlerCalled.set(true));
Completable c3 = Single.just(1)
.flatMapCompletable(i -> Completable.error(new RuntimeException("some exception")));
c3.get();
assertThat(wasHooksHandlerCalled.get(), is(false)); //fails
}
I did some digging and I think this is happening due to a bug in SafeCompletableSubscriber, where it calls RxJavaHooks.onError(e) before it attempts to pass the error in to the wrapped subscribers onError method. ie
public void onError(Throwable e) {
RxJavaHooks.onError(e);
if (done) {
return;
}
done = true;
try {
actual.onError(e);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw new OnErrorFailedException(new CompositeException(e, ex));
}
}
Looking at SafeSubscriber, I think the correct behaviour is to only have the call to RxJavaHooks.onError(e); happen inside the catch block, as no exception has leaked at the point where it is currently being called.
Actually, it is supposed to be inside the if (done) { } block. Would you like to submit a PR?
Closing via #5470.