Retry HTTP requests in angular 6
There are several errors in your code:
- You are shadowing
error
variable - first it's an error stream and then it's an error object. - Using
observableThrowError
withdelay
has no effect. Error will bypass every operator except those dealing with error. - You are mixing "
pipe(operator())
" style operators and prototype style operators.operator()
.
I have suggested some changes:
.handle(customReq).pipe(
tap((ev: HttpEvent<any>) => {
if (ev instanceof HttpResponse) {
console.log('###processing response', ev, this.location);
}
}),
retryWhen(errors => errors
.pipe(
concatMap((error, count) => {
if (count < 5 && (error.status == 400 || error.status == 0)) {
return Observable.of(error.status);
}
return observableThrowError(error);
}),
delay(1000)
)
),
Main change is tracking error count through second argument of concatMap
instead of using take
opeator which is mainly useful for unsubscribing the observable and I think you want throw an error instead.