从角度服务通过管道传输时,rxjs catchError 不起作用

2024-04-12

使用 redux 和 Angular,我有以下效果:

  @Effect()
  public authenticate: Observable<Action> = this.actions
    .ofType(AUTHENTICATE)
    .pipe(
      map((action: AuthenticateAction) => action.payload),
      switchMap(payload => {
        return this.userService.authenticateUser(payload.email, payload.password).pipe(
          map(auth => new AuthenticationSuccessAction({ auth: auth })),
          catchError(error => of(new HttpErrorAction({ error: error })))
        );
      })
    );

服务:

  public authenticateUser(email: string, password: string): Observable<IAuthentication> {
    const formData: FormData = new FormData();
    formData.append('email', email);
    formData.append('password', password);
    return this.httpClient.post('/api/auths/useraccounts', formData) as Observable<IAuthentication>;
  }

当 POST 失败时,永远不会调度 HttpErrorAction。


结果我忘记了我有一个 HttpInterceptor,我捕获了 http 错误,如下所示:

return next.handle(authReq).pipe(catchError(
      (error) => {
        return of(error);
      }

如果我想让错误达到这样的效果,我会这样做:

return Observable.throw(error);

使用较新版本的 rxjs 进行更新:

return throwError(error);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从角度服务通过管道传输时,rxjs catchError 不起作用 的相关文章

随机推荐