import {Injectable} from '@angular/core'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse, } from '@angular/common/http'; import {Observable, throwError} from 'rxjs'; import {NGXLogger} from 'ngx-logger'; import {catchError} from 'rxjs/operators'; @Injectable() export class ServiceHandlerInterceptor implements HttpInterceptor { constructor(private readonly logger: NGXLogger) {} intercept( request: HttpRequest, next: HttpHandler, ): Observable> { return next.handle(request).pipe( // Fixes the issue of errors dropping into "toPromise()" // and being not able to catch it in the "caller methods" catchError((error: HttpErrorResponse) => { const errorMessage = error.error instanceof ErrorEvent ? `Error: ${error.error.message}` : `Error Code: ${error.status}, Message: ${error.message}`; this.logger.error(errorMessage); return throwError(error); }), ); } }