I'm trying to use retryWhen
in HTTP calls.
It works perfectly when try to use like this:
return this.http.get(`${environment.apiUrl}/track/${this.user.instance._id}/${this.currentPlayer.playlist.id}/next?s=${this.playerCounter}`, options)
.timeout(500, new TimeoutError(`Timeout trying to get next track. [instanceId=${this.user.instance._id}]`))
.retryWhen(attempts => {
return Observable.range(1, 3).zip(attempts, i => i).flatMap(i => 3 === i ? Observable.throw(attempts) : Observable.timer(i * 1000));
})
It makes a maximum of 3 tries if get a Timeout error.
But, always have a buuut, I want to make this more abstract to use on various use cases and for this, I have to check the type of the error.
Only TechnicalErros will be retried.
So I tried this without success.
.retryWhen(attempts => {
return attempts.flatMap(error => {
if(error instanceof TechnicalError) {
return Observable.range(1, 3).zip(attempts, i => i).flatMap(i => 3 === i ? Observable.throw(attempts) : Observable.timer(i * 1000));
} else {
Observable.throw(error);
}
});
})
It stops at first try and does not execute the Observable.timer()
, neither the Observable.throw()
.
I have almost sure that the problem is about the first flatMap
, I already tried to use mergeMap
, without success.
Thanks in advance!