【问题标题】:RxJS: Subject errors trickling down to subscribersRxJS:主题错误渗透到订阅者
【发布时间】:2018-11-18 19:54:20
【问题描述】:

我的Subject 正在返回错误,但订阅SubjectObservable 的错误方法正在执行,即使订阅Observable 没有失败并且返回错误。像这样:

this.createPayment$ = this.wizard$.pipe(
  switchMap((wizard) => willReturnAnErrorObservable()),
  share()
);

this. handleSuccessSubscription = this.createPayment$.pipe(
  filter((someEvent: SomeEvent) => event.succeeded),
  switchMap((someEvent: SomeEvent) =>
      // this is never called because the filter above does not pass
      doesNotFailObservable()
  )
).subscribe((confirmedPayment: PaymentDeviceEvent) => {
  // not important
},
  (error) => {
  // this error block is still being called, even though the this.
  // handleSuccessSubscription switchMap is not executed, because the 
  // this.handleSuccessSubscription$ above returns an error
});

在这段代码中,createPaymentSuccessfulSubscription 的错误方法正在执行,即使 switchMap 返回 ok,这是因为“外部”Subject 正在抛出错误。

有什么方法可以防止订阅者也收到错误消息?这并不重要,因为我可以过滤返回的error 以确定是否需要在这里处理;但它有点臭,我希望这个错误块根本不执行。

【问题讨论】:

  • 你能提供stackbiz吗,真的没有从中得到什么

标签: typescript rxjs reactive-programming


【解决方案1】:

您应该在错误传递给最终消费者之前捕获并处理它们

import { of } from 'rxjs';
import { catchError, switchMap, share } from 'rxjs/operators';

this.createPayment$ = this.wizard$.pipe(
  switchMap((wizard) => willReturnAnErrorObservable()),
  catchError((error) => of(console.log('log error', error))),
  share(),
);

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多