spring-retry icon indicating copy to clipboard operation
spring-retry copied to clipboard

Allow for RecoveryCallback to throw original exception

Open shollander opened this issue 6 years ago • 8 comments

The current design of the RetryTemplate and RecoveryCallback is to not throw any exception if a recovery callback is provided. There are cases where even after recovery, the caller wants to know if the original action failed or succeeded. This can be accomplished by providing a way for the recovery callback to throw the original exception. It is currently a little clunky to do so since RecoveryCallback has a signature of throws Exception, while retryContext.getLastThrowable() provides a Throwable, which is not compatible.

shollander avatar Dec 05 '18 23:12 shollander

I guess we could change the signature if we are going to bump to a new major version. Would that solve your problem? Under what circumstances would you care? Why is Exception not broad enough?

dsyer avatar Dec 15 '18 11:12 dsyer

Because this will not compile:

RecoveryCallback<T> = retryContext -> {
    throw retryContext.getLastThrowable();
};

shollander avatar Dec 16 '18 03:12 shollander

Fair enough. But it's easy to work around isn't it? I guess that's what you meant by "clunky".

dsyer avatar Dec 16 '18 08:12 dsyer

Exactly. It would require a lot of type checking and casting, etc.

RecoveryCallback<T> = retryContext -> {
    Throwable t = retryContext.getLastThrowable();
    if (t instanceof Exception) {
        throw (Exception)t;
    } else if (t instanceof Throwable) {
        throw new RuntimeException(t);
    }
};

So basically it requires six lines instead of one, and also requires wrapping the exception in the case of Throwable, which can make it more difficult to deal with later on.

shollander avatar Dec 17 '18 03:12 shollander

Sure, but you would put those 6 lines in a convenience class and only use one line in practice, if this pattern was common. We can put such a convenience class in spring-retry if you like, but there are plenty already out there. Plus it’s easy to write your own.

dsyer avatar Dec 17 '18 06:12 dsyer

Granted, although that doesn't address the second issue of unnecessarily wrapping the original exception in certain cases.

shollander avatar Dec 18 '18 04:12 shollander

Do you have an actual use case where that matters?

dsyer avatar Dec 18 '18 12:12 dsyer

Not at the moment. My point was just that the API is slightly inconsistent here and can cause inconveniences. If you don't want to fix it there are workarounds.

shollander avatar Dec 19 '18 04:12 shollander