Bent Rasmussen
Bent Rasmussen

Reputation: 5702

Swallowing IObservable exceptions

Is it possible to design an Rx operator that swallows repeated exceptions in the same sequence (not the same as Retry or Catch)? In essence the same as .Select(x => x) but ignoring exceptions along the way. (I know this is against guidelines).

Upvotes: 2

Views: 202

Answers (2)

James World
James World

Reputation: 29786

The short answer is "no".

The slightly less short answer is that your premise is flawed; this is because you can't have an Observable throw more than one error, and the operator you are hypothesizing about would be an Observer. Observers, by definition, shouldn't affect Observables (just don't tell Heisenberg I said that).

Therefore, such an operator is logically impossible since it would need to change history - go back and prevent your Observable from ever having thrown an exception. Unless of course your Observable is breaking the rules of Rx.

Don't make it do that. :) Rx will do a pretty good job of preventing this anyway. Subject<T>, for example, just won't publish after the first OnError.

Upvotes: 6

Brandon
Brandon

Reputation: 39192

All of the Rx operators follow the guidelines (they guarantee that you will only receive a single error and then the observable will be done). So any observable that is created via the Reactive library will never be coaxed into allowing multiple errors through.

You would need to write your own custom implementation of IObservable<T> and your own custom operator (that did not make use of any of the Reactive operators) to achieve what you want.

But I do not advise going down that route without further design work. What are you really trying to achieve and are you possibly modelling the problem wrong? If you are expecting multiple errors then perhaps you should be sending those errors down as data through an Observable instead of as an exceptional condition. ie catch the exception at the source and send it to an IObserver<Exception> via its OnNext method.

Upvotes: 2

Related Questions