summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/utils/httpInterceptor/httpInterceptor.service.ts
blob: daa31a33f5514e152e268f09d79d54170c0cc271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {Injectable} from '@angular/core';
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';

import {Observable} from 'rxjs';
import {ErrorMessage, ErrorService} from '../../components/error/error.component.service';
import {SpinnerComponent, SpinnerInfo} from '../../components/spinner/spinner.component';
import {of} from "rxjs";

@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (request.headers.get('x-show-spinner') !== false.toString()) {
      let spinnerInfo : SpinnerInfo = new SpinnerInfo(true, request.url, request.responseType);
      SpinnerComponent.showSpinner.next(spinnerInfo);
    }

    return next.handle(request)
      .catch((err: HttpErrorResponse) => {
        if (err.status === 500) {
          const errorMessage: ErrorMessage = new ErrorMessage('Server not available',
            'It appears that one of the backend servers is not responding.\n Please try later.',
            500);
          ErrorService.showErrorWithMessage(errorMessage);
          return of(null);
        }
        return Observable.throw(err);
      }).finally(() => {
        let spinnerInfo : SpinnerInfo = new SpinnerInfo(false, request.url, request.responseType);
        SpinnerComponent.showSpinner.next(spinnerInfo);
      });
  }
}