John Twigg
John Twigg

Reputation: 7581

ReactiveX : Error Handling that doesn't destroy observable

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?

Upvotes: 7

Views: 618

Answers (2)

dres
dres

Reputation: 1211

If you just add retry() to the source observable, the subscriber does not need to resubscribe.

Upvotes: 0

xsveda
xsveda

Reputation: 17884

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.

Upvotes: 2

Related Questions