import { HTTP_INTERCEPTORS , HttpErrorResponse , HttpEvent , HttpHandler , HttpInterceptor , HttpRequest , } from '@angular/common/http' ; import { ErrorHandler , Injectable , Injector , Provider } from '@angular/core' ; import { Observable } from 'rxjs' ; import { tap } from 'rxjs/operators' ; /** Passes HttpErrorResponse to application-wide error handler */ @ Injectable ( ) export class HttpErrorInterceptor implements HttpInterceptor { static provider : Provider = { provide : HTTP_INTERCEPTORS , useClass : HttpErrorInterceptor , multi : true , } ; constructor ( private injector ) { } intercept ( request , next ) { return next . handle ( request ) . pipe ( tap ( null , ( err ) => { if ( err instanceof HttpErrorResponse ) { const appErrorHandler = this . injector . get ( ErrorHandler ) ; appErrorHandler . handleError ( err ) ; } } ) ) ; } }