Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactiveX : Error Handling that doesn't destroy observable

Tags:

reactivex

Its unclear how to propagate errors to subscribers in REactiveX such that the Observable doesn't get destroyed.

Example

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

I accept this restriction as it is, however I still have scenario where I want listeners downstream to know an error occured AND I don't want the observable to die.

The main use case for this is UI code that, if an error comes through, I don't want to have to call "Setup" against all the observables it previously registered with.

Possible alternatives are

a) push a custom object that has a data field and an error field

class Data
{
    int value;
    Error * error;  
}

I don't like this solution

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?

like image 631
John Twigg Avatar asked Jun 22 '16 20:06

John Twigg


1 Answers

I would definitely go with option A) - create an object that can carry both data and/or error. I doesn't matter how you will wrap the data and possible error into that object but sending both through one stream as onNext() events is the right solution that gives to subscribers all info and all freedom to handle that.

The B) option might be quite challenging to implement in more complex asynchronous scenarios and would probably lead to use of a lot of Subjects which is also bad.

like image 89
xsveda Avatar answered Nov 03 '22 13:11

xsveda