简体   繁体   English

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

[英]rxjs catchError not working when piped from angular service

Using redux and Angular, I have the following effect:使用 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 })))
        );
      })
    );

The service:服务:

  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>;
  }

When the POST fails, the HttpErrorAction never gets dispatched.当 POST 失败时,HttpErrorAction 永远不会被调度。

Turns out I forgot I had an HttpInterceptor that I caught http errors like so:结果我忘记了我有一个 HttpInterceptor ,我捕获了这样的 http 错误:

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

If I wanted to bubble the error up to the effect i'd do something like:如果我想将错误冒泡到效果,我会执行以下操作:

return Observable.throw(error);

Update with newer versions of rxjs:使用较新版本的 rxjs 进行更新:

return throwError(error);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM