如何在行为主题上抛出错误并继续流? [英] How do I throw an error on a behaviour subject and continue the stream?
问题描述
一方面,我有一个可能偶尔会抛出错误的流:
On one end, I have a stream which may occasionally throw an error:
this.behaviorSubject.error(error)
不过,稍后我想继续直播:
Later on, however, I want to continue the stream:
this.behaviorSubject.next(anotherValue)
另一方面,我有一个订阅者订阅了 behaviorSubject.asObservable()
.
on the other end, I have a subscriber subscribed to behaviorSubject.asObservable()
.
在订阅中,我正在处理值和错误:
In the subscribtion, I'm handling the value and the error:
.subscribe(
( value ) => { /* ok */ },
( error ) => { /* some error */ }
);
我希望效果与简单的 onSuccess
和 onError
回调相同,其中每次发生错误时都会调用 onError
并且不会阻止将来进行 onSuccess
调用.我如何使用 RXJS 做到这一点?
I want the effect to be the same as a simple onSuccess
and onError
callback, where onError
is called every time an error occurs and doesn't prevent future onSuccess
calls from being made. How do I do this with RXJS?
我已经研究了 catch ,但它似乎只是防止在订阅者上调用错误.
I've looked into catch but it seems to just prevent error from being called on subscribers.
推荐答案
简短回答:不可能.
如何处理: RxJS 的基本概念是任何 error
或 complete
调用基本上都会杀死 一个流.这个概念迫使您不要随心所欲地到处抛出错误",而是要正确处理应用程序中的错误和数据流.例如,BehaviorSubject
通常用于保存数据,但它不也应该用于包括检索/创建该数据的过程以及处理可能发生的错误数据的检索.
How to work with this: The basic concept of RxJS is that any error
or complete
-call will basically "kill" a stream. This concept forces you not "just to throw around errors here and there as you please" but to handle errors and the flow of data within your application properly. A BehaviorSubject
for example is typically meant to hold data, however it should not be used to also include the process of retrieving/creating that data and handle possible errors that might occur during the retrieval of the data.
因此,如果您想循规蹈矩,您应该将流程分成两部分:
So if you want to go by the book, you should split up your flow into two parts:
- 数据的检索/创建:一个流,将运行一次,然后在发生时完成和/或抛出错误.检索到数据后,会将其发送到商店.
- 商店(例如在你的情况下:一堆 BehaviorSubjects):只有有效数据到达商店,这意味着这里没有进行错误处理,所有部分都依赖于商店可以相信商店拥有正确的数据.
- Retrieval/creation of the data: A stream, that will run once then then completes and/or throws an error whenever one occurs. When the data is retrieved it will be sent to the store.
- The store (e.g. as in your case: a bunch of BehaviorSubjects): Only valid data arrives in the store, this means that no error-handling is done here and all parts relying on the store can trust in the store that it holds the correct data.
<小时>
例如,您的数据流可能如下所示(粗略草图):
As an example your data flow could look as follows (as a rough sketch):
store.ts
dataStore: BehaviorSubject<IData> = new BehaviorSubject<IData>();
errorMessage: BehaviorSubject<IErrorMsg> = new BehaviorSubject<IErrorMsg>();
data-retrieval.ts
fetchDataById(id: string) {
httpService.get(`some/rest/endpoint/${id}`)
.subscribe(handleData, handleError);
}
handleData(data: IData) {
errorMessage.next(null);
dataStore.next(data);
}
handleError(error: Error) {
errorMessage.next(error.message);
dataStore.next(null);
}
<小时>
但这看起来像很多开销..." - 没错,但是它确保了应用程序中干净且易于理解的数据流,易于测试和维持.还有现成的商店概念,如 ngrx 或 redux 可以使用.
"But this looks like a lot of overhead..." - True, however it ensures a clean and easy-to-understand flow of data within your application, that is easy to test and maintain. Also there are ready-to-use store-concepts like ngrx or redux that could be used.
这篇关于如何在行为主题上抛出错误并继续流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!