0

I had a clarification on usage of intercepting ErrorHandler for handling custom error for HTTP requests and client side errors in Angular 6+.

Its getting called correctly for client side errors. But for HTTP errors custom error handler not getting called when there is a error handler added HTTP request subscriber(See below code). Same time custom error handler get called when error handler removed from subscriber. Is that expected behavior. Couldn't find any doc related to that in Angular doc.

.subscribe(
  success => {
    this.processGetChart(success);
  },
  error => {
    this.errors = error;
    console.log('API Error: ', error);
  },
  () => {
  }
  );

Thanks,

Peter

2
  • Sorry, but your question is very unclear. Try to reword it completely. A good pattern is: I'm executing this code: <code>. I expect the following to happen: <...>. But instead here's what happens: <...>. Commented Mar 17, 2019 at 13:59
  • Yes, it is expected that when you handle the error then you have caught it and it will not go any further unless you rethrow it. Commented Mar 20, 2019 at 21:26

1 Answer 1

1

You can have an HttpInterceptor

And inside, you catch the different types of error.

like this one :

@Injectable()
export class customInterceptor implements HttpInterceptor {

  constructor() {
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(request).pipe(
      tap((event: HttpEvent<any>) => {
        }, (err: any) => {
          if (err instanceof HttpErrorResponse) {
            if (err.status === 403 || err.status === 401) {
              // DO SOMETHING HERE.
            }
          }
        }
      )
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.