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:
  1. swallow(catch) the error and switch over to a backup Observable to continue the sequence
  2. swallow the error and emit a default item
  3. swallow the error and immediately try to restart the failed Observable
  4. 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:

  1. doOnError
  2. onErrorResumeNext
  3. onErrorReturn
  4. onErrorReturnItem
  5. onExceptionResumeNext
  6. Retry
  7. retryUntil
  8. retryWhen

doOnError() Operator: 

Available in :  Flowable, Observable, Maybe, Single and Completable

  1. Quite similar to doOnNext operator.
  2. User to intercept error before reaching to consumer.
  3. It called before onError of observable.
  4. It’s a great place to show some toast message and snackbar about the error.
  5. public final Observable <T> doOnError(Consumer<? super Throwable> onError)

onErrorResumeNext() Operator : 


Available in : Flowable, Observable, Maybe, Single and Completable

  1. onErrorResumeNext replaces the current stream with an entirely new Observable.
  2. In onErrorResumeNext() operator, it handles the throwable generated by Java code
  3. It is used to emit different set of stream if encounter error in observable stream before it their way to observer method.


Once error occurred in observable stream, control goes to on onErrorResumeNext  block where we can subscribe to other observable to provide some data.



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 : 


Available in : Flowable, Observable, Maybe and Single

  1. Instructs a reactive type to emit the item returned by the specified,when it encounters an error.
  2. We sometimes need to produce the default item when an error or exception . 
  3. So onErrorReturn provides us a throwable and a lambda to return.


onErrorReturnItem() Operator :


Available in :  Flowable, Observable, Maybe and Single

  1. Instructs a reactive type to emit a particular item when it encounters an error.
  2. This is particularly useful no matter is error coming, we have some default value in return.
  3. 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 : 


Available in : Flowable, Observable and Maybe

  1. Instructs an Observable Source to pass control to another Observable Source rather than invoking onError if it encounters an Exception.
  2. public final Observable<T> onExceptionResumeNext(ObservableSource<? extends T> next)


retry() Operator : 


Available in :  Flowable, Observable, Maybe, Single and Completable
  1. simply call re-subscribes Observable
  2. Means resubscribe to the source reactive type if it encounters an error in the hopes that it will complete without error.
  3. 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 : 


Available in : Flowable, Observable and Maybe

  • 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 : 


Available in : Flowable, Observable, Maybe, Single and Completable


  1. Instructs a reactive type to pass any error to another Observable or Flowable to determine whether to re-subscribe to the source.












Comments

Post a Comment

Popular posts from this blog

Android Interview question

A complete guide to learn Android Development