-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: Add Single.onErrorResumeNext(Func) #3766
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package rx.internal.operators; | ||
|
||
import rx.Single; | ||
import rx.SingleSubscriber; | ||
import rx.exceptions.Exceptions; | ||
import rx.functions.Func1; | ||
import rx.plugins.RxJavaPlugins; | ||
|
||
public class SingleOperatorOnErrorResumeNext<T> implements Single.OnSubscribe<T> { | ||
|
||
private final Single<? extends T> originalSingle; | ||
private final Func1<Throwable, ? extends Single<? extends T>> resumeFunctionInCaseOfError; | ||
|
||
private SingleOperatorOnErrorResumeNext(Single<? extends T> originalSingle, Func1<Throwable, ? extends Single<? extends T>> resumeFunctionInCaseOfError) { | ||
if (originalSingle == null) { | ||
throw new NullPointerException("originalSingle must not be null"); | ||
} | ||
|
||
if (resumeFunctionInCaseOfError == null) { | ||
throw new NullPointerException("resumeFunctionInCaseOfError must not be null"); | ||
} | ||
|
||
this.originalSingle = originalSingle; | ||
this.resumeFunctionInCaseOfError = resumeFunctionInCaseOfError; | ||
} | ||
|
||
public static <T> SingleOperatorOnErrorResumeNext<T> withFunction(Single<? extends T> originalSingle, Func1<Throwable, ? extends Single<? extends T>> resumeFunctionInCaseOfError) { | ||
return new SingleOperatorOnErrorResumeNext<T>(originalSingle, resumeFunctionInCaseOfError); | ||
} | ||
|
||
public static <T> SingleOperatorOnErrorResumeNext<T> withOther(Single<? extends T> originalSingle, final Single<? extends T> resumeSingleInCaseOfError) { | ||
if (resumeSingleInCaseOfError == null) { | ||
throw new NullPointerException("resumeSingleInCaseOfError must not be null"); | ||
} | ||
|
||
return new SingleOperatorOnErrorResumeNext<T>(originalSingle, new Func1<Throwable, Single<? extends T>>() { | ||
@Override | ||
public Single<? extends T> call(Throwable throwable) { | ||
return resumeSingleInCaseOfError; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void call(final SingleSubscriber<? super T> child) { | ||
final SingleSubscriber<? super T> parent = new SingleSubscriber<T>() { | ||
@Override | ||
public void onSuccess(T value) { | ||
child.onSuccess(value); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
try { | ||
resumeFunctionInCaseOfError.call(error).subscribe(child); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd separate catching the error and subscribing to the Single outside the try-catch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's terminal state, we don't do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind. I just found |
||
} catch (Throwable innerError) { | ||
Exceptions.throwOrReport(innerError, child); | ||
} | ||
} | ||
}; | ||
|
||
child.add(parent); | ||
originalSingle.subscribe(parent); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1220,13 +1220,78 @@ public void onErrorResumeNextViaSingleShouldPreventNullSingle() { | |
try { | ||
Single | ||
.just("value") | ||
.onErrorResumeNext(null); | ||
.onErrorResumeNext((Single<String>) null); | ||
fail(); | ||
} catch (NullPointerException expected) { | ||
assertEquals("resumeSingleInCaseOfError must not be null", expected.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void onErrorResumeNextViaFunctionShouldNotInterruptSuccesfulSingle() { | ||
TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); | ||
|
||
Single | ||
.just("success") | ||
.onErrorResumeNext(new Func1<Throwable, Single<? extends String>>() { | ||
@Override | ||
public Single<? extends String> call(Throwable throwable) { | ||
return Single.just("fail"); | ||
} | ||
}) | ||
.subscribe(testSubscriber); | ||
|
||
testSubscriber.assertValue("success"); | ||
} | ||
|
||
@Test | ||
public void onErrorResumeNextViaFunctionShouldResumeWithPassedSingleInCaseOfError() { | ||
TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); | ||
|
||
Single | ||
.<String> error(new RuntimeException("test exception")) | ||
.onErrorResumeNext(new Func1<Throwable, Single<? extends String>>() { | ||
@Override | ||
public Single<? extends String> call(Throwable throwable) { | ||
return Single.just("fallback"); | ||
} | ||
}) | ||
.subscribe(testSubscriber); | ||
|
||
testSubscriber.assertValue("fallback"); | ||
} | ||
|
||
@Test | ||
public void onErrorResumeNextViaFunctionShouldPreventNullFunction() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also check/handle if the function returns a null Single. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test, but didn't handle this specifically in the operator, test will make sure that it won't be swallowed |
||
try { | ||
Single | ||
.just("value") | ||
.onErrorResumeNext((Func1<Throwable, ? extends Single<? extends String>>) null); | ||
fail(); | ||
} catch (NullPointerException expected) { | ||
assertEquals("resumeFunctionInCaseOfError must not be null", expected.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void onErrorResumeNextViaFunctionShouldFailIfFunctionReturnsNull() { | ||
try { | ||
Single | ||
.error(new TestException()) | ||
.onErrorResumeNext(new Func1<Throwable, Single<? extends String>>() { | ||
@Override | ||
public Single<? extends String> call(Throwable throwable) { | ||
return null; | ||
} | ||
}) | ||
.subscribe(); | ||
|
||
fail(); | ||
} catch (OnErrorNotImplementedException expected) { | ||
assertTrue(expected.getCause() instanceof NullPointerException); | ||
} | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void iterableToArrayShouldThrowNullPointerExceptionIfIterableNull() { | ||
Single.iterableToArray(null); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just renamed
SingleOperatorOnErrorResumeNextViaSingle
to something more general and it's now works with function instead ofSingle
directly.