Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I not retry on error 400 and just console.log the error?

@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;
      })
    );
  }

>Solution :

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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;
      })
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading