Here is a simple interceptor written you can write if you want to get any specific status code from any request (like 401) and trying to recall the last API

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req).pipe(catchError((err: any) => {
            if(err.status == 401) {
                return next.handle(req).pipe(retry(1));
            } else {
                return throwError(err);
            }
        }));
    }

In this exemple we catch all error then :
– We are trying to call one more time the last API who generated a 401 with retry(1)
– Otherwise, we throw the error that we can use for any other case (in a subscribe, service,…)

Leave a Reply

Your email address will not be published. Required fields are marked *