Error handling operator
Why Error Handling:
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(catch) 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
Case Study/Scenario of failure :
Flow and what exception can cause ? Error” is an event which might happen in a stream.
That “Error event” is terminal event which propagated downstream through different operator towards the observer. That means that after error happened stream is basically finished and no more events can come through it.
Consumers are required to handle errors in the onErrorr callback of Observer.
If Consumer didn’t handle error in Observer callback, then that error is sent to a global error handler (which in case of Android crashes the app by default).
NOTE: some errors which happen inside stream can go directly to global error handler e.g. in cases when the stream is already disposed.
Not a single error can escape chain. All the exceptions will go to the onError callback of Observer (or global error handler).
List of error handling operators:
- doOnError
- onErrorResumeNext
- onErrorReturn
- onErrorReturnItem
- onExceptionResumeNext
- Retry
- retryUntil
- retryWhen
Available in : Flowable, Observable, Maybe, Single and Completable
- Quite similar to doOnNext operator.
- User to intercept error before reaching to consumer.
- It called before onError of observable.
- It’s a great place to show some toast message and snackbar about the error.
- public final Observable <T> doOnError(Consumer<? super Throwable> onError)
onErrorResumeNext() Operator :
Available in : Flowable, Observable, Maybe, Single and Completable
- onErrorResumeNext replaces the current stream with an entirely new Observable.
- In onErrorResumeNext() operator, it handles the throwable generated by Java code
- It is used to emit different set of stream if encounter error in observable stream before it their way to observer method.
onErrorResumeNext operatore is combination of concat and catch operator that means it b catches the error and appending some other observable source to previous observable.
onErrorReturn() Operator :
- Instructs a reactive type to emit the item returned by the specified,when it encounters an error.
- We sometimes need to produce the default item when an error or exception .
- So onErrorReturn provides us a throwable and a lambda to return.
onErrorReturnItem() Operator :
Available in : Flowable, Observable, Maybe and Single
- Instructs a reactive type to emit a particular item when it encounters an error.
- This is particularly useful no matter is error coming, we have some default value in return.
- The positioning of onErrorReturnItem matters as if we call it above doOnNext, it wouldn't be called after the Observable Exception and then onError will be called directly
onExceptionResumeNext() Operator :
- Instructs an Observable Source to pass control to another Observable Source rather than invoking onError if it encounters an Exception.
- public final Observable<T> onExceptionResumeNext(ObservableSource<? extends T> next)
retry() Operator :
Available in : Flowable, Observable, Maybe, Single and Completable
- simply call re-subscribes Observable
- Means resubscribe to the source reactive type if it encounters an error in the hopes that it will complete without error.
- Handling retry is quite trick,because if observable is used for some intensive operation so resubscribing could cause some issue,so better used inside flatmap to limit the scope. public final Observable<T> retry()
- While emitting data If the source ObservableSource calls Observer.onError(java.lang.Throwable).
- Retry will subscribe to source obserable, rather then propgating to the onError call
- For example, if an ObservableSource fails at first but emits [1, 2] then succeeds the second time and emits [1, 2, 3, 4, 5] then the complete sequence of emissions and notifications would be [1, 2, 1, 2, 3, 4, 5, onComplete]
retryUntil() Operator :
- Instructs a reactive type to resubscribe to the source reactive type if it encounters an error until the returns true.
- public final Observable<T> retryUntil(BooleanSuplier stop)
retryWhen() Operator :
- Instructs a reactive type to pass any error to another Observable or Flowable to determine whether to re-subscribe to the source.
Nice
ReplyDelete