error.interceptor.ts 737 Bytes
import {HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/catch';


export class ErrorInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<HttpEventType.Response>> {
    return next.handle(req).catch(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 Observable.throw(error);
  }
}