From 9e625ee36a0a43cdda10f38b34af3540fadf22aa Mon Sep 17 00:00:00 2001 From: Lorenz Pahl Date: Sun, 28 Oct 2018 19:58:33 +0100 Subject: [PATCH 1/5] Change document structure; update operator list --- docs/Error-Handling-Operators.md | 92 +++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 7 deletions(-) diff --git a/docs/Error-Handling-Operators.md b/docs/Error-Handling-Operators.md index 8acae59787..b2eade5151 100644 --- a/docs/Error-Handling-Operators.md +++ b/docs/Error-Handling-Operators.md @@ -1,14 +1,92 @@ -There are a variety of operators that you can use to react to or recover from `onError` notifications from Observables. For example, you might: +There are a variety of operators that you can use to react to or recover from `onError` notifications from reactive sources, such as `Observable`s. For example, you might: 1. swallow the error and switch over to a backup Observable to continue the sequence 1. swallow the error and emit a default item 1. swallow the error and immediately try to restart the failed Observable 1. swallow the error and try to restart the failed Observable after some back-off interval -The following pages explain these operators. +# Outline -* [**`onErrorResumeNext( )`**](http://reactivex.io/documentation/operators/catch.html) — instructs an Observable to emit a sequence of items if it encounters an error -* [**`onErrorReturn( )`**](http://reactivex.io/documentation/operators/catch.html) — instructs an Observable to emit a particular item when it encounters an error -* [**`onExceptionResumeNext( )`**](http://reactivex.io/documentation/operators/catch.html) — instructs an Observable to continue emitting items after it encounters an exception (but not another variety of throwable) -* [**`retry( )`**](http://reactivex.io/documentation/operators/retry.html) — if a source Observable emits an error, resubscribe to it in the hopes that it will complete without error -* [**`retryWhen( )`**](http://reactivex.io/documentation/operators/retry.html) — if a source Observable emits an error, pass that error to another Observable to determine whether to resubscribe to the source \ No newline at end of file +- [`doOnError`](#doonerror) +- [`onErrorComplete`](#onerrorcomplete) +- [`onErrorResumeNext`](#onerrorresumenext) +- [`onErrorReturn`](#onerrorreturn) +- [`onErrorReturnItem`](#onerrorreturnitem) +- [`onExceptionResumeNext`](#onexceptionresumenext) +- [`retry`](#retry) +- [`retryUntil`](#retryuntil) +- [`retryWhen`](#retrywhen) + +## doOnError + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/do.html](http://reactivex.io/documentation/operators/do.html) + +When the source reactive type signals an error event, the given `io.reactivex.functions.Consumer` is invoked. + +## onErrorComplete + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/catch.html](http://reactivex.io/documentation/operators/catch.html) + +When the reactive type signals an error event, the error will be swallowed and replaced by a complete event. + +Optionally, a `io.reactivex.functions.Predicate` can be specified that gives more control over when an error event should be replaced by a complete event, and when not. + +## onErrorResumeNext + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/catch.html](http://reactivex.io/documentation/operators/catch.html) + +Instructs a reactive type to emit a sequence of items if it encounters an error. + +## onErrorReturn + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/catch.html](http://reactivex.io/documentation/operators/catch.html) + +Instructs a reactive type to emit the item returned by the specified `io.reactivex.functions.Function` when it encounters an error. + +## onErrorReturnItem + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/catch.html](http://reactivex.io/documentation/operators/catch.html) + +Instructs a reactive type to emit a particular item when it encounters an error. + +## onExceptionResumeNext + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/catch.html](http://reactivex.io/documentation/operators/catch.html) + +Instructs a reactive type to continue emitting items after it encounters an `java.lang.Exception`. Unlike [`onErrorResumeNext`](#onerrorresumenext), this one lets other types of `Throwable` continue through. + +## retry + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/retry.html](http://reactivex.io/documentation/operators/retry.html) + +If a source reactive type emits an error, resubscribe to it in the hopes that it will complete without error. + +## retryUntil + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/retry.html](http://reactivex.io/documentation/operators/retry.html) + +If a source reactive type emits an error, resubscribe to it until the given `io.reactivex.functions.BooleanSupplier` returns `true`. + +## retryWhen + +**Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` + +**ReactiveX documentation:** [http://reactivex.io/documentation/operators/retry.html](http://reactivex.io/documentation/operators/retry.html) + +If a source reactive type emits an error, pass that error to another `Observable` or `Flowable` to determine whether to resubscribe to the source. From 2039e6e95c18d9cefc6e7b687b090968e9c5466f Mon Sep 17 00:00:00 2001 From: Lorenz Pahl Date: Mon, 29 Oct 2018 11:42:57 +0100 Subject: [PATCH 2/5] Add examples --- docs/Error-Handling-Operators.md | 192 +++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) diff --git a/docs/Error-Handling-Operators.md b/docs/Error-Handling-Operators.md index b2eade5151..033eddfbbe 100644 --- a/docs/Error-Handling-Operators.md +++ b/docs/Error-Handling-Operators.md @@ -25,6 +25,17 @@ There are a variety of operators that you can use to react to or recover from `o When the source reactive type signals an error event, the given `io.reactivex.functions.Consumer` is invoked. +### doOnError example + +```java +Observable.error(new IOException("Something went wrong")) + .doOnError(error -> System.err.println("The error message is: " + error.getMessage())) + .subscribe( + x -> System.out.println("This should never be printed!"), + Throwable::printStackTrace, + () -> System.out.println("This should never be printed!")); +``` + ## onErrorComplete **Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` @@ -35,6 +46,19 @@ When the reactive type signals an error event, the error will be swallowed and r Optionally, a `io.reactivex.functions.Predicate` can be specified that gives more control over when an error event should be replaced by a complete event, and when not. +### onErrorComplete example + +```java +Completable.fromAction(() -> { + throw new IOException(); +}).onErrorComplete(error -> { + // only ignore errors of type java.io.IOException + return error instanceof IOException; +}).subscribe( + () -> System.out.println("IOException was ignored"), + error -> System.err.println("This should not be printed!")); +``` + ## onErrorResumeNext **Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` @@ -43,6 +67,36 @@ Optionally, a `io.reactivex.functions.Predicate` can be specified that gives mor Instructs a reactive type to emit a sequence of items if it encounters an error. +### onErrorResumeNext example + +```java + Observable numbers = Observable.generate(() -> 1, (state, emitter) -> { + emitter.onNext(state); + + return state + 1; +}); + +numbers.scan(Math::multiplyExact) + .onErrorResumeNext(Observable.empty()) + .subscribe( + System.out::println, + error -> System.err.println("This should not be printed!")); + +// prints: +// 1 +// 2 +// 6 +// 24 +// 120 +// 720 +// 5040 +// 40320 +// 362880 +// 3628800 +// 39916800 +// 479001600 +``` + ## onErrorReturn **Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` @@ -51,6 +105,22 @@ Instructs a reactive type to emit a sequence of items if it encounters an error. Instructs a reactive type to emit the item returned by the specified `io.reactivex.functions.Function` when it encounters an error. +### onErrorReturn example + +```java +Single.just("2A") + .map(v -> Integer.parseInt(v, 10)) + .onErrorReturn(error -> { + if (error instanceof NumberFormatException) return 0; + else throw new IllegalArgumentException(); + }) + .subscribe( + System.out::println, + error -> System.err.println("This should not be printed!")); + +// prints 0 +``` + ## onErrorReturnItem **Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` @@ -59,6 +129,19 @@ Instructs a reactive type to emit the item returned by the specified `io.reactiv Instructs a reactive type to emit a particular item when it encounters an error. +### onErrorReturnItem example + +```java +Single.just("2A") + .map(v -> Integer.parseInt(v, 10)) + .onErrorReturnItem(0) + .subscribe( + System.out::println, + error -> System.err.println("This should not be printed!")); + +// prints 0 +``` + ## onExceptionResumeNext **Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` @@ -67,6 +150,25 @@ Instructs a reactive type to emit a particular item when it encounters an error. Instructs a reactive type to continue emitting items after it encounters an `java.lang.Exception`. Unlike [`onErrorResumeNext`](#onerrorresumenext), this one lets other types of `Throwable` continue through. +### onExceptionResumeNext example + +```java +Observable exception = Observable.error(IOException::new) + .onExceptionResumeNext(Observable.just("This value will be used to recover from the IOException")); + +Observable error = Observable.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:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` @@ -75,6 +177,30 @@ Instructs a reactive type to continue emitting items after it encounters an `jav If a source reactive type emits an error, resubscribe to it in the hopes that it will complete without error. +### retry example + +```java +Observable source = Observable.interval(0, 1, TimeUnit.SECONDS) + .flatMap(x -> { + if (x >= 2) return Observable.error(new IOException("Something went wrong!")); + else return Observable.just(x); + }); + +source.retry((retryCount, error) -> retryCount < 3) + .blockingSubscribe( + x -> System.out.println("onNext: " + x), + error -> System.err.println("onError: " + error.getMessage())); + +// prints: +// onNext: 0 +// onNext: 1 +// onNext: 0 +// onNext: 1 +// onNext: 0 +// onNext: 1 +// onError: Something went wrong! +``` + ## retryUntil **Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_off.png) `Completable` @@ -83,6 +209,32 @@ If a source reactive type emits an error, resubscribe to it in the hopes that it If a source reactive type emits an error, resubscribe to it until the given `io.reactivex.functions.BooleanSupplier` returns `true`. +### retryUntil example + +```java +LongAdder errorCounter = new LongAdder(); +Observable source = Observable.interval(0, 1, TimeUnit.SECONDS) + .flatMap(x -> { + if (x >= 2) return Observable.error(new IOException("Something went wrong!")); + else return Observable.just(x); + }) + .doOnError((error) -> errorCounter.increment()); + +source.retryUntil(() -> errorCounter.intValue() >= 3) + .blockingSubscribe( + x -> System.out.println("onNext: " + x), + error -> System.err.println("onError: " + error.getMessage())); + +// prints: +// onNext: 0 +// onNext: 1 +// onNext: 0 +// onNext: 1 +// onNext: 0 +// onNext: 1 +// onError: Something went wrong! +``` + ## retryWhen **Available in:** ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Flowable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Observable`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Maybe`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Single`, ![image](https://raw.github.com/wiki/ReactiveX/RxJava/images/checkmark_on.png) `Completable` @@ -90,3 +242,43 @@ If a source reactive type emits an error, resubscribe to it until the given `io. **ReactiveX documentation:** [http://reactivex.io/documentation/operators/retry.html](http://reactivex.io/documentation/operators/retry.html) If a source reactive type emits an error, pass that error to another `Observable` or `Flowable` to determine whether to resubscribe to the source. + +### retryWhen example + +```java +Observable source = Observable.interval(0, 1, TimeUnit.SECONDS) + .flatMap(x -> { + if (x >= 2) return Observable.error(new IOException("Something went wrong!")); + else return Observable.just(x); + }); + +source.retryWhen(errors -> { + return errors.map(error -> 1) + + // count the number of errors + .scan(Math::addExact) + + .doOnNext(errorCount -> System.out.println("No. of errors: " + errorCount)) + + // limit the maximum number of retries + .takeWhile(errorCount -> errorCount < 3) + + // signal resubscribe event after some delay + .flatMapSingle(errorCount -> Single.timer(errorCount, TimeUnit.SECONDS)); +}).blockingSubscribe( + x -> System.out.println("onNext: " + x), + Throwable::printStackTrace, + () -> System.out.println("onComplete")); + +// prints: +// onNext: 0 +// onNext: 1 +// No. of errors: 1 +// onNext: 0 +// onNext: 1 +// No. of errors: 2 +// onNext: 0 +// onNext: 1 +// No. of errors: 3 +// onComplete +``` From b9a334f50e77c276a66d12c9ac087d61e18e8a98 Mon Sep 17 00:00:00 2001 From: Lorenz Pahl Date: Tue, 30 Oct 2018 11:59:20 +0100 Subject: [PATCH 3/5] Clarify which handler is invoked --- docs/Error-Handling-Operators.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/Error-Handling-Operators.md b/docs/Error-Handling-Operators.md index 033eddfbbe..dbe2778d2b 100644 --- a/docs/Error-Handling-Operators.md +++ b/docs/Error-Handling-Operators.md @@ -31,9 +31,9 @@ When the source reactive type signals an error event, the given `io.reactivex.fu Observable.error(new IOException("Something went wrong")) .doOnError(error -> System.err.println("The error message is: " + error.getMessage())) .subscribe( - x -> System.out.println("This should never be printed!"), + x -> System.out.println("onNext should never be printed!"), Throwable::printStackTrace, - () -> System.out.println("This should never be printed!")); + () -> System.out.println("onComplete should never be printed!")); ``` ## onErrorComplete @@ -56,7 +56,7 @@ Completable.fromAction(() -> { return error instanceof IOException; }).subscribe( () -> System.out.println("IOException was ignored"), - error -> System.err.println("This should not be printed!")); + error -> System.err.println("onError should not be printed!")); ``` ## onErrorResumeNext @@ -80,7 +80,7 @@ numbers.scan(Math::multiplyExact) .onErrorResumeNext(Observable.empty()) .subscribe( System.out::println, - error -> System.err.println("This should not be printed!")); + error -> System.err.println("onError should not be printed!")); // prints: // 1 @@ -116,7 +116,7 @@ Single.just("2A") }) .subscribe( System.out::println, - error -> System.err.println("This should not be printed!")); + error -> System.err.println("onError should not be printed!")); // prints 0 ``` @@ -137,7 +137,7 @@ Single.just("2A") .onErrorReturnItem(0) .subscribe( System.out::println, - error -> System.err.println("This should not be printed!")); + error -> System.err.println("onError should not be printed!")); // prints 0 ``` From a68e6c804ef7904efd32eb13f21cf7a0c1ae4183 Mon Sep 17 00:00:00 2001 From: Lorenz Pahl Date: Tue, 30 Oct 2018 12:16:45 +0100 Subject: [PATCH 4/5] Use consistent wording in the descriptions --- docs/Error-Handling-Operators.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/Error-Handling-Operators.md b/docs/Error-Handling-Operators.md index dbe2778d2b..d77d537b8d 100644 --- a/docs/Error-Handling-Operators.md +++ b/docs/Error-Handling-Operators.md @@ -23,7 +23,7 @@ There are a variety of operators that you can use to react to or recover from `o **ReactiveX documentation:** [http://reactivex.io/documentation/operators/do.html](http://reactivex.io/documentation/operators/do.html) -When the source reactive type signals an error event, the given `io.reactivex.functions.Consumer` is invoked. +Instructs a reactive type to invoke the given `io.reactivex.functions.Consumer` when it encounters an error. ### doOnError example @@ -42,9 +42,9 @@ Observable.error(new IOException("Something went wrong")) **ReactiveX documentation:** [http://reactivex.io/documentation/operators/catch.html](http://reactivex.io/documentation/operators/catch.html) -When the reactive type signals an error event, the error will be swallowed and replaced by a complete event. +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 complete event, and when not. +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 @@ -175,7 +175,7 @@ Observable.concat(exception, error) **ReactiveX documentation:** [http://reactivex.io/documentation/operators/retry.html](http://reactivex.io/documentation/operators/retry.html) -If a source reactive type emits an error, resubscribe to it in the hopes that it will complete without error. +Instructs a reactive type to resubscribe to the source reactive type if it encounters an error in the hopes that it will complete without error. ### retry example @@ -207,7 +207,7 @@ source.retry((retryCount, error) -> retryCount < 3) **ReactiveX documentation:** [http://reactivex.io/documentation/operators/retry.html](http://reactivex.io/documentation/operators/retry.html) -If a source reactive type emits an error, resubscribe to it until the given `io.reactivex.functions.BooleanSupplier` returns `true`. +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`. ### retryUntil example @@ -241,7 +241,7 @@ source.retryUntil(() -> errorCounter.intValue() >= 3) **ReactiveX documentation:** [http://reactivex.io/documentation/operators/retry.html](http://reactivex.io/documentation/operators/retry.html) -If a source reactive type emits an error, pass that error to another `Observable` or `Flowable` to determine whether to resubscribe to the source. +Instructs a reactive type to pass any error to another `Observable` or `Flowable` to determine whether to resubscribe to the source. ### retryWhen example From 0cc7585c5a85889058d280fb73fa43569c9c6b29 Mon Sep 17 00:00:00 2001 From: Lorenz Pahl Date: Tue, 30 Oct 2018 13:54:53 +0100 Subject: [PATCH 5/5] Cleanup --- docs/Error-Handling-Operators.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/Error-Handling-Operators.md b/docs/Error-Handling-Operators.md index d77d537b8d..0b2eace61f 100644 --- a/docs/Error-Handling-Operators.md +++ b/docs/Error-Handling-Operators.md @@ -52,7 +52,7 @@ Optionally, a `io.reactivex.functions.Predicate` can be specified that gives mor Completable.fromAction(() -> { throw new IOException(); }).onErrorComplete(error -> { - // only ignore errors of type java.io.IOException + // Only ignore errors of type java.io.IOException. return error instanceof IOException; }).subscribe( () -> System.out.println("IOException was ignored"), @@ -255,15 +255,15 @@ Observable source = Observable.interval(0, 1, TimeUnit.SECONDS) source.retryWhen(errors -> { return errors.map(error -> 1) - // count the number of errors + // Count the number of errors. .scan(Math::addExact) .doOnNext(errorCount -> System.out.println("No. of errors: " + errorCount)) - // limit the maximum number of retries + // Limit the maximum number of retries. .takeWhile(errorCount -> errorCount < 3) - // signal resubscribe event after some delay + // Signal resubscribe event after some delay. .flatMapSingle(errorCount -> Single.timer(errorCount, TimeUnit.SECONDS)); }).blockingSubscribe( x -> System.out.println("onNext: " + x),