Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw exception after reaching max attempts in resilience4j-retry

How I can throw an exception when maximum number of retries is reached. In my case when Response has other code than 200 I would like to throw exception.

Retry retry = RetryRegistry.of(
  RetryConfig.<Response> custom()
    .retryOnResult({ it.statusCode() != 200 })
    .build())
  .retry("my-retry")


Response response = Retry.decorateSupplier(retry, { foo.bar() }).get()
like image 504
pixel Avatar asked Dec 09 '25 09:12

pixel


1 Answers

you can wrap your code and throw an exception when the HTTP code is not 200.

For example in Java code:

Supplier<Response> supplier= () -> foo.bar();
Supplier<String> supplierWithResultHandling = SupplierUtils.andThen(supplier, result -> {
   if (result.statusCode().is4xxClientError()) {
      throw new HttpClientErrorException(result.statusCode());
   } else if (result.statusCode().is5xxServerError())  {
      throw new HttpServerErrorException(result.statusCode());
   }
  return result;
});

Response response = retry.executeSupplier(supplierWithResultHandling);
like image 181
Robert Winkler Avatar answered Dec 11 '25 16:12

Robert Winkler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!