I am trying to implement a request/response intercept mechanism in Angular4 .I am fairly new to observables.
I maintain two interceptor array , one for request and other for response. Interceptors are nothing but functions which accept request/response object and transforms them.
sendRequest(req:Response):Observable<Response>{
req= this.processRequest(req);
This.http.request(req)
.map( (res:Response)=>{
return this.processResponse(res)
})
.catch(this.handleError)
}
handleError(err:Response):Observable<Response>{
return Observable.throw(err);
}
Basic error handling works fine . Sometimes for a 401 exception , I want to get new Auth token and retry the same request with updated Auth token.
What I am thinking is to introduce array of error interceptor. One of the error interceptor function would check if it is 401 and would issue a new refresh request to server ,invalidating subsequent error interceptor functions . I assume there is a need to switch the observable stream. The observer will eventually get response from latest request made. How to proceed on this?