retry4j icon indicating copy to clipboard operation
retry4j copied to clipboard

Get last exception

Open jflefebvre06 opened this issue 4 years ago • 5 comments

Could you please add a way to get the last exception from a BackoffStrategy.

jflefebvre06 avatar Aug 05 '20 10:08 jflefebvre06

@jflefebvre06 - You can already check the last exception that generated a retry by looking in the Status object - look in the documentation here and let me know if this already covers what you are asking for: https://github.com/elennick/retry4j#call-status

elennick avatar Nov 27 '20 18:11 elennick

No, Status is not accessible from BackoffStrategy

public interface BackoffStrategy {

    Duration getDurationToWait(int numberOfTriesFailed, Duration delayBetweenAttempts);

    default void validateConfig(RetryConfig config) {}

}

jflefebvre06 avatar Nov 27 '20 18:11 jflefebvre06

Could you share an example of what you're trying to get access to? Do you have your own custom BackoffStrategy implementation that contains logic that can throw an exception?

elennick avatar Nov 27 '20 19:11 elennick

The use case is to manage http too many requests

Duration getDurationToWait(int numberOfTriesFailed, Duration delayBetweenAttempts) {

   if(exception.getHttpStatus()==429)
       return Duration.ofMinutes(1);
   else
       return Duration.ofSeconds(2);
}

jflefebvre06 avatar Nov 27 '20 19:11 jflefebvre06

@jflefebvre06 ok I understand... you want to be able to have more granular retry control over a single callable. Retry4j does not support this type of case at the moment. I'm not sure that implementing it inside a backoff strategy would be the correct approach... a better design would probably be to allow multiple retry configurations for a single callable, so that different configs could handle different exception types or return values. Please feel free to take a shot at an implementation and open a PR if you would like. Let me know if you want to discuss the design further before you do so. Thanks!

elennick avatar Nov 27 '20 23:11 elennick