interceptor - HttpInterceptor in Angular 4.3: Intercepting 400 error responses -


i to intercept 401 , other errors in order react accordingly. interceptor:

import { loggingservice } './../logging/logging.service'; import { injectable } '@angular/core'; import { httpinterceptor, httphandler, httprequest, httpevent, httpresponse, httperrorresponse } '@angular/common/http';  import { observable } 'rxjs/observable'; import 'rxjs/add/operator/do';  @injectable() export class twshttpinterceptor implements httpinterceptor {      constructor(private logger: loggingservice) { }      intercept(request: httprequest<any>, next: httphandler): observable<httpevent<any>> {         this.logger.logdebug(request);             return next.handle(request)             .do(event => {                 if (event instanceof httpresponse) {                     this.logger.logdebug(event);                 }             });     } } 

while works 200 requests, it not intercept error respsonses

all see in chrome's dev console this:

zone.js:2616 http://localhost:8080/backend/rest/wrongurl 404 (not found)

or this

zone.js:2616 http://localhost:8080/backend/rest/url 401 (unauthorized)

i interceptor deal this. missing ?

http sends errors down error stream of observable need catch them .catch (you can read more here).

return next.handle(request)   .do(event => {     if (event instanceof httpresponse) {       this.logger.logdebug(event);     }   })   .catch(err => {      console.log('caught error', err);     return observable.throw(err);   }); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -