I have an angular2 error interceptor that catches all failures from my HTTP abstraction, my issue is when I'm catching the error and returning a new observable, the observables passed to .flatMap() are not being fired.
Here's an example of my http client implementation that makes the request:
return this._http.get(endpoint, data || {}, opts)
.map(this.responseInterceptor)
.catch(error => this.errorInterceptor({
method: method,
endpoint: endpoint,
data: data || {},
options: options
}, error));
Here's the error interceptor
/**
*
* @param request
* @param error
* @returns {any}
*/
errorInterceptor(request: any, error: any): any {
if (error.status === 401) {
let tokenRequest = this.getToken({refresh_token: localStorage.getItem('refresh')}, 'refresh_token').map(res => {
localStorage.setItem('token', res.access_token);
localStorage.setItem('refresh', res.refresh_token);
request.data.headers.set('Authorization', 'Bearer ' + res.access_token)
})
return tokenRequest.flatMap(res => this.request(request.method, request.endpoint, {}, request.data, false));
}
return this.handleError(error);
}
The issue is only the token request is being called, the request passed to .flatMap()
never gets called.
I can work around the issue by doing:
tokenRequest.subscribe(res => {
// logic here
return this.request(...);
});
However, then the response structure changes which requires me to change code to handle pre-expiration requests and post-expiration, which is something I'd like to avoid.