Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x: Add Completable.onErrorResumeWith #6868

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/io/reactivex/rxjava3/core/Completable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,33 @@ public final Completable onErrorResumeNext(@NonNull Function<? super Throwable,
Objects.requireNonNull(fallbackSupplier, "fallbackSupplier is null");
return RxJavaPlugins.onAssembly(new CompletableResumeNext(this, fallbackSupplier));
}
/**
* Resumes the flow with the given {@link CompletableSource} when the current {@code Completable} fails instead of
* signaling the error via {@code onError}.
* <p>
* <img width="640" height="409" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.onErrorResumeWith.png" alt="">
* <p>
* You can use this to prevent errors from propagating or to supply fallback data should errors be
* encountered.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code onErrorResumeWith} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param fallback
* the next {@code CompletableSource} that will take over if the current {@code Completable} encounters
* an error
* @return the new {@code Completable} instance
* @throws NullPointerException if {@code fallback} is {@code null}
* @see <a href="http://reactivex.io/documentation/operators/catch.html">ReactiveX operators documentation: Catch</a>
*/
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable onErrorResumeWith(@NonNull CompletableSource fallback) {
Objects.requireNonNull(fallback, "fallback is null");
return onErrorResumeNext(Functions.justFunction(fallback));
}

/**
* Nulls out references to the upstream producer and downstream {@link CompletableObserver} if
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/reactivex/rxjava3/core/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4084,7 +4084,7 @@ public final Maybe<T> onErrorComplete(@NonNull Predicate<? super Throwable> pred
* Resumes the flow with the given {@link MaybeSource} when the current {@code Maybe} fails instead of
* signaling the error via {@code onError}.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/onErrorResumeNext.png" alt="">
* <img width="640" height="298" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.onErrorResumeWith.png" alt="">
* <p>
* You can use this to prevent errors from propagating or to supply fallback data should errors be
* encountered.
Expand Down Expand Up @@ -4112,7 +4112,7 @@ public final Maybe<T> onErrorResumeWith(@NonNull MaybeSource<? extends T> fallba
* Resumes the flow with a {@link MaybeSource} returned for the failure {@link Throwable} of the current {@code Maybe} by a
* function instead of signaling the error via {@code onError}.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/onErrorResumeNext.png" alt="">
* <img width="640" height="298" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.onErrorResumeNext.png" alt="">
* <p>
* You can use this to prevent errors from propagating or to supply fallback data should errors be
* encountered.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@

package io.reactivex.rxjava3.internal.operators.completable;

import static org.mockito.Mockito.*;
import org.junit.Test;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.testsupport.TestHelper;

public class CompletableResumeNextTest extends RxJavaTest {

@Test
public void resumeWithError() {
public void resumeNextError() {
Completable.error(new TestException())
.onErrorResumeNext(Functions.justFunction(Completable.error(new TestException("second"))))
.to(TestHelper.<Object>testConsumer())
Expand Down Expand Up @@ -58,4 +59,28 @@ public void disposed() {
.onErrorResumeNext(Functions.justFunction(Completable.never()))
);
}

@Test
public void resumeWithNoError() throws Throwable {
Action action = mock(Action.class);

Completable.complete()
.onErrorResumeWith(Completable.fromAction(action))
.test()
.assertResult();

verify(action, never()).run();
}

@Test
public void resumeWithError() throws Throwable {
Action action = mock(Action.class);

Completable.error(new TestException())
.onErrorResumeWith(Completable.fromAction(action))
.test()
.assertResult();

verify(action).run();
}
}