redux-observable catchError 后不会执行

redux-observable wont be execute after catchError

我在 angular 项目中使用 redux-observable。我的 epics 中有一个 updateNode 函数,带有 catchError 处理。我多次发送操作 DB_UPDATE_NODE 并且一切正常。然而,当我再次调度该动作时,时间动作执行 catchErrorupdateNode 函数将不再被调用。

public updateNode(action$: Observable<IScenarioNodesAction>, store: Store<IAppState>) {
  return action$.pipe(
    ofType(ScenarioNodeActionType.DB_UPDATE_NODE),
    flatMap((action) => {
     return  this._scenarioNodeService.update(action.payload.botId);
    }),
    flatMap(data => [
      this._scenarioNodeAction.updateNode(data),
    ]),
    catchError((error) => { 
      return Observable.of(this._commonAction.showError(error));
    })
  );
}

如果您的 observable returns 出现错误,它会阻止观察者获取新事件。

请向 catchError 添加如下内容:

  public updateNode(action$: Observable<IScenarioNodesAction>, store: Store<IAppState>){
    return action$.pipe(
      ofType(ScenarioNodeActionType.DB_UPDATE_NODE),
      flatMap((action) => {
       return  this._scenarioNodeService.update(action.payload.botId);
      }),
        flatMap(data => [
          this._scenarioNodeAction.updateNode(data),
        ]) ,
        catchError((error) => { 
              retryWhen((errors) => {
               return errors
                .pipe(delay(1000), take(1))
                .concat(Observable.throw(`Error`));
            },
        ),
      })
    );

  } 

上面的操作等待一秒钟 delay(1000) 并重试一定次数 take(1)。如果错误仍然存​​在,则会抛出错误 .concat(Observable.throw('Error'))。在您的情况下,您只需调用 showErrordrop 连接即可。

您必须在服务流中捕获错误。

flatMap((action) => {
  return this._scenarioNodeService.update(action.payload.botId)
             .pipe(catchError(...)) ;
})

有关详细信息,请参阅 docs

Here we placed the catchError() inside our mergeMap(), but after our AJAX call; this is important because if we let the error reach the action$.pipe(), it will terminate it and no longer listen for new actions.