Angular 4.3 HttpClient Interceptor -


please advise why route redirection not working when response 401. console.log shows response object , status 401 router not redirecting user logout page.

intercept(req: httprequest<any>, next: httphandler): observable<httpevent<any>> {              this.onstart();             return next.handle(req).do((event: httpevent<any>) =>                 (error: any) => {                     if (error instanceof httperrorresponse) {                         if (error.status == 0 || error.status == 401 || error.status == 404 || error.status == 500 || error.status == 503) {                             this.storageservice.writetostorage(constants.storage_key_error, error);                             console.log(error);                             this.router.navigatebyurl(constants.route_error_dynamic + error.status);                         }                     }                     else{                         return observable.throw(error);                     }                  }).finally(() =>{                     this.onend();                 })         } 

you can try this

import { router } '@angular/router'; import { observable } 'rxjs'; import {injectable} '@angular/core'; import {httpevent, httpinterceptor, httphandler, httprequest} '@angular/common/http'; import { httperrorresponse } "@angular/common/http";  @injectable() export class angularinterceptor implements httpinterceptor {    constructor(private router : router){}    intercept(req: httprequest<any>, next: httphandler): observable<httpevent<any>> {     return next.handle(req).do(event => {}, err => {         if(err instanceof httperrorresponse){             console.log("error caught interceptor");             //observable.throw(err);             this.router.navigate(['a4']);         }     });   } } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -