import { ErrorHandlingService } from './../../shared/services/error-handling/error-handling.service'; import { HttpHandler, HttpRequest, HttpEvent, HttpErrorResponse, HttpInterceptor } from '@angular/common/http'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { Injectable, Injector } from '@angular/core'; @Injectable() export class HttpErrorInterceptor implements HttpInterceptor { constructor( private injector: Injector, ) {} intercept( request: HttpRequest, next: HttpHandler ): Observable> { return next.handle(request).pipe( catchError((error: HttpErrorResponse) => { // We don't inject an HttpClient dependent service directly to an http interceptor's constructor, // or we'll get cyclic dependency errors const errorHandlingService = this.injector.get(ErrorHandlingService); errorHandlingService.showHttpResponseError(error); return throwError(error); }) ) as Observable>; } }