2

I would like to have a HTTPInterceptor to handle all Errors. My code looks like this:

return next.handle(req)
  .pipe(
    catchError((response: any) => {
      if (response instanceof HttpErrorResponse) {
        if (response.status === 401) {
          console.log('error catched';

          return empty();
        }
      }

      return throwError(response);
    })
  );

but when running a code which produces a 401 error

return this.http
  .post(`${environment.baseAPIUrl}/identity/login`, { 'user': user, 'pwd': pwd})
  .pipe(
    tap(res => this.setToken(res)),
    shareReplay()
  );

the output is like that:

POST http://xyz 401 (Unauthorized)
error catched

so I can handle the error but I can't prevent the error output. What am I doing wrong?

best

Ingmar

Ingmar
  • 21
  • 3

1 Answers1

0

Have you tried putting a debugger statement in and then stepping through the code to see if it's still hitting the line following line?

return throwError(response);

I agree with you, that it seems it shouldn't be bubbling up the error if it's not hitting that line.

Uniphonic
  • 845
  • 12
  • 21