import { Injectable } from '@angular/core' ; import { HttpEvent , HttpInterceptor , HttpHandler , HttpRequest , HttpResponse , HttpErrorResponse } from '@angular/common/http' ; import { AlertController } from 'ionic-angular' ; import { Observable } from "rxjs/Observable" ; import 'rxjs/add/operator/do' ; @ Injectable ( ) export class TimingInterceptor implements HttpInterceptor { constructor ( private alertCtrl ) { } intercept ( req , next ) { const started = Date . now ( ) ; return next . handle ( req ) . do ( event => { if ( event instanceof HttpResponse ) { const elapsed = Date . now ( ) - started ; console . log ( `---> Request for ${ req . urlWithParams } took ${ elapsed } ms.` ) ; console . log ( '---> Status:' , event . status ) ; } } , ( err ) => { this . handleError ( err ) ; } ) ; } handleError ( err ) { if ( err . error instanceof Error ) { console . log ( 'An error occurred:' , err . error . message ) ; } else { console . log ( `Backend returned code ${ err . status }, body was:` , err . error ) ; } } presentAlert ( message ) { let alert = this . alertCtrl . create ( { title : 'Error' , subTitle : message , buttons : [ 'OK' ] } ) ; alert . present ( ) ; } }