-3

Show code

 this.authService
  .login(payload)
  .pipe(
    map((e: any) => e.token),
    tap(e => dispatch(new AuthLoginSuccess(e)))
  )
  .subscribe();

i am trying to show the error returning from nestjs backend to my angular frontend. The above function returns the token if the correct credentials are posted. I am not able to obtain the errors

Achu
  • 3
  • 1
  • 6
  • Please clarify the question, it's not clear exactly what you're trying to accomplish. – Matt U Dec 16 '19 at 05:47
  • Please add more description to your question and show more code. What does `distpatch(new AuthLoginSuccess(e))` do? What types are there? What does `this.authService.login(payload)` return? What does a mock payload look like? – Jay McDoniel Dec 16 '19 at 05:48
  • am trying to get if any error from nestjs backend to angular frontend – Achu Dec 16 '19 at 05:48
  • 1
    https://angular.io/guide/http#error-handling – Saurabh Yadav Dec 16 '19 at 05:50
  • distpatch(new AuthLoginSuccess(e)) is used to do some function after login is success – Achu Dec 16 '19 at 05:51
  • please refer to https://stackoverflow.com/help/how-to-ask and clearly state your problem, your question is not clear. – Hiras Haris Dec 16 '19 at 05:58
  • Add an `error` handler to the subscription i.e `this.authService .login(payload) .pipe( map((e: any) => e.token), tap(e => dispatch(new AuthLoginSuccess(e))) ) .subscribe(() => {// success handler}, err => {// error handler});` – Nicholas K Dec 16 '19 at 06:38

2 Answers2

1

With RxJS to catch any errors you need to use the catchError() operator, similarly to how you are using tap and map. The other option is to provide an error observer to the subscribe() method and handle it there. Depending on what you want to do with the error, both can be useful.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
0
  .login(payload)
  .pipe(
    map((e: any) => e.token),
    tap(e => dispatch(new AuthLoginSuccess(e))),
    catchError(error => { .... do stuff .... } )
  )
  .subscribe();

This should allow you to grab any errors from your service.

Chris Hailey
  • 199
  • 1
  • 4