Search code examples
angularerror-handlingobservableangular-http-interceptors

How can I count the number of time that was executed retry operator wether the exception of an error occurred


@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    constructor() { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(request)
            .pipe(
                retry(3),
                catchError((error: HttpErrorResponse) => {
                    let errorMessage = '';
                    if (error.error instanceof ErrorEvent) {
                        // client-side error
                        errorMessage = `Error: ${error.error.message}`;
                    } else {
                        // server-side error
                        errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
                    }
                    window.alert(errorMessage);                       
                    return throwError(error);
                }));
    }
}

In case I got an error. I would like to get the number of time the retry operator(retry(3)) was executed, someone can I help me, please!


Solution

  • The retry operator was triggered when you have an error, you just need to count the number of errors you have. This an example here