Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for RecoveryCallback to throw original exception #135

Open
shollander opened this issue Dec 5, 2018 · 8 comments
Open

Allow for RecoveryCallback to throw original exception #135

shollander opened this issue Dec 5, 2018 · 8 comments

Comments

@shollander
Copy link

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.

@dsyer
Copy link
Member

dsyer commented Dec 15, 2018

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?

@shollander
Copy link
Author

Because this will not compile:

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

@dsyer
Copy link
Member

dsyer commented Dec 16, 2018

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

@shollander
Copy link
Author

shollander commented Dec 17, 2018

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.

@dsyer
Copy link
Member

dsyer commented Dec 17, 2018

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.

@shollander
Copy link
Author

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

@dsyer
Copy link
Member

dsyer commented Dec 18, 2018

Do you have an actual use case where that matters?

@shollander
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants