You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
akarnokd edited this page Oct 30, 2018
·
29 revisions
There are a variety of operators that you can use to react to or recover from onError notifications from reactive sources, such as Observables. For example, you might:
swallow the error and switch over to a backup Observable to continue the sequence
swallow the error and emit a default item
swallow the error and immediately try to restart the failed Observable
swallow the error and try to restart the failed Observable after some back-off interval
Instructs a reactive type to invoke the given io.reactivex.functions.Consumer when it encounters an error.
doOnError example
Observable.error(newIOException("Something went wrong"))
.doOnError(error -> System.err.println("The error message is: " + error.getMessage()))
.subscribe(
x -> System.out.println("onNext should never be printed!"),
Throwable::printStackTrace,
() -> System.out.println("onComplete should never be printed!"));
onErrorComplete
Available in:Flowable, Observable, Maybe, Single, Completable
Instructs a reactive type to swallow an error event and replace it by a completion event.
Optionally, a io.reactivex.functions.Predicate can be specified that gives more control over when an error event should be replaced by a completion event, and when not.
onErrorComplete example
Completable.fromAction(() -> {
thrownewIOException();
}).onErrorComplete(error -> {
// Only ignore errors of type java.io.IOException.returnerrorinstanceofIOException;
}).subscribe(
() -> System.out.println("IOException was ignored"),
error -> System.err.println("onError should not be printed!"));
onErrorResumeNext
Available in:Flowable, Observable, Maybe, Single, Completable
Instructs a reactive type to emit the item returned by the specified io.reactivex.functions.Function when it encounters an error.
onErrorReturn example
Single.just("2A")
.map(v -> Integer.parseInt(v, 10))
.onErrorReturn(error -> {
if (errorinstanceofNumberFormatException) return0;
elsethrownewIllegalArgumentException();
})
.subscribe(
System.out::println,
error -> System.err.println("onError should not be printed!"));
// prints 0
onErrorReturnItem
Available in:Flowable, Observable, Maybe, Single, Completable
Instructs a reactive type to emit a particular item when it encounters an error.
onErrorReturnItem example
Single.just("2A")
.map(v -> Integer.parseInt(v, 10))
.onErrorReturnItem(0)
.subscribe(
System.out::println,
error -> System.err.println("onError should not be printed!"));
// prints 0
onExceptionResumeNext
Available in:Flowable, Observable, Maybe, Single, Completable
Instructs a reactive type to continue emitting items after it encounters an java.lang.Exception. Unlike onErrorResumeNext, this one lets other types of Throwable continue through.
onExceptionResumeNext example
Observable<String> exception = Observable.<String>error(IOException::new)
.onExceptionResumeNext(Observable.just("This value will be used to recover from the IOException"));
Observable<String> error = Observable.<String>error(Error::new)
.onExceptionResumeNext(Observable.just("This value will not be used"));
Observable.concat(exception, error)
.subscribe(
message -> System.out.println("onNext: " + message),
err -> System.err.println("onError: " + err));
// prints:// onNext: This value will be used to recover from the IOException// onError: java.lang.Error
retry
Available in:Flowable, Observable, Maybe, Single, Completable
Instructs a reactive type to resubscribe to the source reactive type if it encounters an error until the given io.reactivex.functions.BooleanSupplier returns true.