1

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

luca
  • 3,248
  • 10
  • 66
  • 145
  • `retryWhen` should do what you need. What is the problem again? You don't know how to create the chain or it's the compilation error (in that case show the error message)? – martin Mar 09 '19 at 13:48
  • 1
    Check https://stackoverflow.com/q/54733093/5394220 – Jota.Toledo Mar 09 '19 at 13:48
  • i don't understad how can filter by isntanceof and retry 3 times. Is it possibile to make this for all http request or I have to chain all http call? – luca Mar 09 '19 at 13:54

0 Answers0