Project

General

Profile

1
import { ErrorHandlingService } from './../../shared/services/error-handling/error-handling.service';
2
import {
3
  HttpHandler,
4
  HttpRequest,
5
  HttpEvent,
6
  HttpErrorResponse,
7
  HttpInterceptor
8
} from '@angular/common/http';
9
import { Observable, throwError } from 'rxjs';
10
import { catchError } from 'rxjs/operators';
11
import { Injectable, Injector } from '@angular/core';
12

    
13
@Injectable()
14
export class HttpErrorInterceptor implements HttpInterceptor {
15
  constructor(
16
    private injector: Injector,
17
  ) {}
18

    
19
  intercept(
20
    request: HttpRequest<any>,
21
    next: HttpHandler
22
  ): Observable<HttpEvent<any>> {
23
    return next.handle(request).pipe(
24
      catchError((error: HttpErrorResponse) => {
25
        // We don't inject an HttpClient dependent service directly to an http interceptor's constructor,
26
        // or we'll get cyclic dependency errors
27
        const errorHandlingService = this.injector.get(ErrorHandlingService);
28

    
29
        errorHandlingService.showHttpResponseError(error);
30
        return throwError(error);
31
      })
32
    ) as Observable<HttpEvent<any>>;
33
  }
34
}
(3-3/3)