Search code examples
angularactionngrxeffects

Ngrx: CatchError does not dispatch an action


How to handle catchError with latest ngrx?

effect$ = createEffect(() =>
    this.actions$.pipe(
      ofType(contactAction),
      switchMap(({ data }) =>
        this.postService.contact(data).pipe(
          map(() => contactSuccess()),
          catchError(error => contactFail)
        )
      )
    )
  );
  
  
  
  

Effect does not dispatch "contactFail" action when an HTTP error occurs.


Solution

  • catchError(error => of( contactFail() ))
    

    Should do the trick. The map Operator automatically lifts the return value into an observable, thats why you do not need the of Operator at your successful Part of the request. For CatchError to work, it's return value has to be wrapped into an observable for ngrx to be able to resolve it.