error.interceptor.ts 789 Bytes

import {throwError as observableThrowError, Observable} from 'rxjs';

import {catchError} from 'rxjs/operators';
import {HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';



export class ErrorInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<HttpEventType.Response>> {
    return next.handle(req).pipe(catchError(this.handleException));
  }


  private handleException(error: HttpResponse<any>) {

    console.log('error on MOYA rest connection', error);

    // TODO: add handlers to 403's and other "not logged in" or "invalid permissions"
    // -statuscodes, and route them using some nice global parameter

    return observableThrowError(error);
  }
}