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()
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With