从角度服务管道传输时,rxjs catchError无法正常工作 [英] rxjs catchError not working when piped from angular service

查看:125
本文介绍了从角度服务管道传输时,rxjs catchError无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using redux and Angular, I have the following effect:

  @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.

When the POST fails, the HttpErrorAction never gets dispatched.

推荐答案

结果我忘了我有一个HttpInterceptor,它像这样捕获了HTTP错误:

Turns out I forgot I had an HttpInterceptor that I caught http errors like so:

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

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

Update with newer versions of rxjs:

return throwError(error);

这篇关于从角度服务管道传输时,rxjs catchError无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆