1+ import { RequestError } from './../shared/models/index' ;
2+ import { ToastrService } from 'ngx-toastr' ;
13import { Injectable } from "@angular/core" ;
24import { HttpRequest , HttpHandler , HttpEvent } from "@angular/common/http" ;
35import { Observable } from "rxjs/Rx" ;
@@ -6,23 +8,31 @@ import { Observable } from "rxjs/Rx";
68@Injectable ( )
79export class MyHttpInterceptor {
810 constructor (
11+ private toastr : ToastrService ,
912 ) { }
1013
11- intercept ( req : HttpRequest < any > , next : HttpHandler ) : Observable < HttpEvent < any > > {
14+ intercept (
15+ req : HttpRequest < any > ,
16+ next : HttpHandler
17+ ) : Observable < HttpEvent < any > > {
1218
13- console . log ( "intercepted request ... " ) ;
19+ // console.log("intercepted request ... ");
1420
1521 // Clone the request to add the new header.
1622 const authReq = req . clone ( { headers : req . headers . set ( "headerName" , "headerValue" ) } ) ;
17-
18- console . log ( "Sending request with new header now ..." ) ;
23+
24+ // console.log("Sending request with new header now ...");
1925
2026 //send the newly created request
2127 return next . handle ( authReq )
2228 . catch ( ( error , caught ) => {
2329 //intercept the respons error and displace it to the console
24- console . log ( "Error Occurred" ) ;
25- console . log ( error ) ;
30+ let requestError : RequestError = error ;
31+ if ( requestError . status === 404 ) {
32+ this . toastr . error ( "Request " + requestError . statusText , "Error" ) ;
33+ } else {
34+ this . toastr . error ( "Api Failled" , "Error" ) ;
35+ }
2636 //return the error to the method that called it
2737 return Observable . throw ( error ) ;
2838 } ) as any ;
0 commit comments