that_guy
that_guy

Reputation: 2445

rxjs catchError not working when piped from angular service

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

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.

Upvotes: 11

Views: 12768

Answers (1)

that_guy
that_guy

Reputation: 2445

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

Update with newer versions of rxjs:

return throwError(error);

Upvotes: 22

Related Questions