I'm currently working with the Angular 2 HTTP library, which returns an observable. I'm trying to set up a retry mechanism for specific error statuses/codes.
The problem I'm facing is that when the error status is not 429, Observable.of(error)
gets executed in the error case for retries. However, if all two retries fail, the flow continues to the success block instead of the catch block.
Is there a way to ensure that the flow goes to the catch block when all retry attempts fail?
return this.http.get(url,options)
.retryWhen((errors) => {
return errors
.mergeMap((error) => (error.status === 429) ? Observable.throw(error) : Observable.of(error))
.take(2);
})
.toPromise()
.then((res:Response) => console.log('In Success Block'))
.catch((res) => this.handleError(res));
Do you think this solution will address my issue?
return this.http
.post(url, JSON.stringify(body), requestOptions).retryWhen((errors) => {
return errors
.mergeMap((error) => (error.status === 404) ? Observable.throw(error) : Observable.of(error))
.take(2);
}).map((res:Response) =>{
if (res.status === 200)
return res;
else
return Observable.throw(res);
})
.toPromise();