As I delve into Angular4, my focus lies on creating a request/response intercept mechanism. Despite being new to observables, I am determined to implement this feature.
In order to achieve this, I have established two arrays for interceptors – one for requests and the other for responses. Interceptors essentially consist of functions that take in request/response objects and modify them as needed.
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);
}
While the basic error handling is operational, there are instances when I encounter a 401 exception and require a new Auth token to retry the same request with the updated token.
My current approach involves introducing an array of error interceptors. One such function within these error interceptors would specifically address a 401 status code by initiating a refresh request to obtain a new token, thereby overriding any subsequent error interceptor functions. I anticipate needing to switch the observable stream accordingly, ultimately ensuring the observer receives the response from the most recent request made. How should I proceed with this?