Handling Exceptions in Reactive Extensions without stopping sequence

后端 未结 3 987
野趣味
野趣味 2020-12-10 14:51

Why RX has the following grammar OnNext* (OnError|OnCompleted)? instead of (OnNext|OnError)* OnCompleted? This is quite clear from implementation p

3条回答
  •  没有蜡笔的小新
    2020-12-10 15:27

    If the consumer is the one that knows if the Exception is expected or not, then I say that throwing exceptions is incorrect here.

    You're essentially trying to convey state or logic, which exceptions are not meant to do. Exceptions are meant to bring the system (or at the very least, the current operation) to a grinding halt because something has happened which the code doesn't inherently know how to recover from.

    In this case, it just so happens that an Exception is part of your business logic/state, but that doesn't mean you can throw them. You need to pass them downstream to your consumer and then have your consumer process them.

    A Tuple would work in a pinch here, but the lack of specificity around the type (as well as the properties) is usually messy, so I'd recommend creating a dedicated type to convey the results of your operation and is exposed through the IObservable.

提交回复
热议问题