uoa-repository-manager-service/app/core/error-handling/http-error.interceptor.ts

35 lines
1.1 KiB
TypeScript

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<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
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<HttpEvent<any>>;
}
}