I'm new in angular and I would like to retry the http call if there is a network interruption or some error between client and server. At the moment I have this configuration like suggested by official documentation
return this.http.get<ApiResult<Atto[]>>(this.apiEnv + "Atti")
.pipe(
retry(3), // retry a failed request up to 3 times
catchError(err => this.handleError.handleError(err, {severity:'error', summary:'Impossibile recuperare gli atti', life:5000})) // then handle the error;
);
this is the error handler:
public handleError(error: HttpErrorResponse, message : Message) {
this.toastService.addSingle(message);
return throwError('Something bad happened; please try again later.');
};
I have an interceptor for the authentication or authorization error:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
//no authentication
if (err.status === 401) {
this.router.navigate(['login']);
}
//no authorization
if (err.status === 403) {
this.auth.logout().subscribe(
() => this.auth.logoutSuccess()
);
}
const error = err.error.message || err.statusText;
return throwError(error);
}))
}
The problem is that all my http retry 3 times even if the problem is on the server so it is useless (futhermore the console print the handler message), I would like to retry only for communication error.
I read about retryWhen
and error.error instanceof ErrorEvent
to catch client-side or network error. There is a way to make all together, I can't change retry
with retryWhen
with the check of error instance without compiler error