Skip to content

Commit b157b86

Browse files
authored
3.x: Add Completable.onErrorResumeWith (#6868)
1 parent ad1840b commit b157b86

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/main/java/io/reactivex/rxjava3/core/Completable.java

+27
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,33 @@ public final Completable onErrorResumeNext(@NonNull Function<? super Throwable,
20862086
Objects.requireNonNull(fallbackSupplier, "fallbackSupplier is null");
20872087
return RxJavaPlugins.onAssembly(new CompletableResumeNext(this, fallbackSupplier));
20882088
}
2089+
/**
2090+
* Resumes the flow with the given {@link CompletableSource} when the current {@code Completable} fails instead of
2091+
* signaling the error via {@code onError}.
2092+
* <p>
2093+
* <img width="640" height="409" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.onErrorResumeWith.png" alt="">
2094+
* <p>
2095+
* You can use this to prevent errors from propagating or to supply fallback data should errors be
2096+
* encountered.
2097+
* <dl>
2098+
* <dt><b>Scheduler:</b></dt>
2099+
* <dd>{@code onErrorResumeWith} does not operate by default on a particular {@link Scheduler}.</dd>
2100+
* </dl>
2101+
*
2102+
* @param fallback
2103+
* the next {@code CompletableSource} that will take over if the current {@code Completable} encounters
2104+
* an error
2105+
* @return the new {@code Completable} instance
2106+
* @throws NullPointerException if {@code fallback} is {@code null}
2107+
* @see <a href="http://reactivex.io/documentation/operators/catch.html">ReactiveX operators documentation: Catch</a>
2108+
*/
2109+
@CheckReturnValue
2110+
@NonNull
2111+
@SchedulerSupport(SchedulerSupport.NONE)
2112+
public final Completable onErrorResumeWith(@NonNull CompletableSource fallback) {
2113+
Objects.requireNonNull(fallback, "fallback is null");
2114+
return onErrorResumeNext(Functions.justFunction(fallback));
2115+
}
20892116

20902117
/**
20912118
* Nulls out references to the upstream producer and downstream {@link CompletableObserver} if

src/main/java/io/reactivex/rxjava3/core/Maybe.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4084,7 +4084,7 @@ public final Maybe<T> onErrorComplete(@NonNull Predicate<? super Throwable> pred
40844084
* Resumes the flow with the given {@link MaybeSource} when the current {@code Maybe} fails instead of
40854085
* signaling the error via {@code onError}.
40864086
* <p>
4087-
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/onErrorResumeNext.png" alt="">
4087+
* <img width="640" height="298" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.onErrorResumeWith.png" alt="">
40884088
* <p>
40894089
* You can use this to prevent errors from propagating or to supply fallback data should errors be
40904090
* encountered.
@@ -4112,7 +4112,7 @@ public final Maybe<T> onErrorResumeWith(@NonNull MaybeSource<? extends T> fallba
41124112
* Resumes the flow with a {@link MaybeSource} returned for the failure {@link Throwable} of the current {@code Maybe} by a
41134113
* function instead of signaling the error via {@code onError}.
41144114
* <p>
4115-
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/onErrorResumeNext.png" alt="">
4115+
* <img width="640" height="298" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.onErrorResumeNext.png" alt="">
41164116
* <p>
41174117
* You can use this to prevent errors from propagating or to supply fallback data should errors be
41184118
* encountered.

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableResumeNextTest.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313

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

16+
import static org.mockito.Mockito.*;
1617
import org.junit.Test;
1718

1819
import io.reactivex.rxjava3.core.*;
1920
import io.reactivex.rxjava3.exceptions.TestException;
20-
import io.reactivex.rxjava3.functions.Function;
21+
import io.reactivex.rxjava3.functions.*;
2122
import io.reactivex.rxjava3.internal.functions.Functions;
2223
import io.reactivex.rxjava3.testsupport.TestHelper;
2324

2425
public class CompletableResumeNextTest extends RxJavaTest {
2526

2627
@Test
27-
public void resumeWithError() {
28+
public void resumeNextError() {
2829
Completable.error(new TestException())
2930
.onErrorResumeNext(Functions.justFunction(Completable.error(new TestException("second"))))
3031
.to(TestHelper.<Object>testConsumer())
@@ -58,4 +59,28 @@ public void disposed() {
5859
.onErrorResumeNext(Functions.justFunction(Completable.never()))
5960
);
6061
}
62+
63+
@Test
64+
public void resumeWithNoError() throws Throwable {
65+
Action action = mock(Action.class);
66+
67+
Completable.complete()
68+
.onErrorResumeWith(Completable.fromAction(action))
69+
.test()
70+
.assertResult();
71+
72+
verify(action, never()).run();
73+
}
74+
75+
@Test
76+
public void resumeWithError() throws Throwable {
77+
Action action = mock(Action.class);
78+
79+
Completable.error(new TestException())
80+
.onErrorResumeWith(Completable.fromAction(action))
81+
.test()
82+
.assertResult();
83+
84+
verify(action).run();
85+
}
6186
}

0 commit comments

Comments
 (0)