@Injectable()
export class RetryInterceptor implements HttpInterceptor {
private keycloakService = inject(KeycloakService);
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
// console.log(this.keycloakService.getKeycloakInstance()?.tokenParsed);
// console.log(request);
return next.handle(request).pipe(
retry({
count: 3,
delay: (error, retryCount) => {
if (error.status === 503 || error.status === 504) {
return timer(1000); // retry every 1 second for 503 and 504
}
return timer(retryCount * 1000); // Retry with increasing delay
}
}),
catchError((error) => {
if (error.status === 401) {
return this.handleUnauthorized(request, next);
}
if (error.status === 503 || error.status === 504) {
// message that will redirect to the local UI URL if it comes back 503/504.
}
if (!noDisplayError) {
}
throw error;
})
);
}
You can use the new retryWhen
replacement with delay and retry
, where we throw an error when 400 occours, then throw the error. Which we catch using catchError
and log the console message, finally we return an observable, so that the stream does not error out.
@Injectable()
export class RetryInterceptor implements HttpInterceptor {
private keycloakService = inject(KeycloakService);
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
): Observable<HttpEvent<unknown>> {
// console.log(this.keycloakService.getKeycloakInstance()?.tokenParsed);
// console.log(request);
return next.handle(request).pipe(
retry({
count: 3,
delay: (error, retryCount) => {
if (error.status === 503 || error.status === 504) {
return timer(1000); // retry every 1 second for 503 and 504
}
if (error.status === 400) {
return throwError(() => error); // just return an observable with the error.
}
return timer(retryCount * 1000); // Retry with increasing delay
},
}),
catchError((error) => {
if (error.status === 401) {
return this.handleUnauthorized(request, next);
}
if (error.status === 503 || error.status === 504) {
// message that will redirect to the local UI URL if it comes back 503/504.
}
if (error.status === 400) {
console.log('400 error occoured');
return of({});
}
throw error;
})
);
}
}