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

4

1 回答 1

1

你可以有一个HttpInterceptor

在里面,你会发现不同类型的错误。

像这个 :

@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.
            }
          }
        }
      )
    );
  }
}
于 2019-03-17T14:19:32.510 回答