ReactiveX: Error Handling That Does Not Destroy Observables

It is unclear how to spread errors among subscribers in REactiveX so that Observable is not destroyed.

Example

observable.onNext(1); observable.onNext(2); observable.onError("Nope"); observable.onNext(3);<<won't work. 

I accept this limitation as it is, however I still have a scenario where I want the listeners to know downstream that an error has occurred AND ALSO I don't want the watchers to die.

The main use case for this is the user interface code, which, if an error occurs, I do not want to call "Setup" for all the previously observed ones.

Possible alternatives

a) enter user object with data field and error field

 class Data { int value; Error * error; } 

I do not like this decision

b) Have two streams. One for data and one for errors.

 observable.onNext(1); observable.onNext(2); errorObservable.onNext("Error"); observable.onNext(3); 

What are the best common practices for this?

+6
source share
2 answers

I would definitely go with option A) - create an object that can carry both data and / or errors. It does not matter how you wrap the data and the possible error in this object, but sending both through the same thread, since the onNext() event is the right solution, which gives subscribers all the information and all the freedom in it.

Option B) is quite difficult to implement in more complex asynchronous scenarios and is likely to lead to the use of a large number of Subject , which is also bad.

+1
source

If you just add retry () to the watched source, the subscriber does not need to re-subscribe.

0
source

All Articles