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

Add method to RetryException to get a typed last Attempt #224

Closed
sleberknight opened this issue Jun 5, 2024 · 1 comment · Fixed by #225
Closed

Add method to RetryException to get a typed last Attempt #224

sleberknight opened this issue Jun 5, 2024 · 1 comment · Fixed by #225
Assignees
Labels
enhancement A request for change or improvement to an existing feature
Milestone

Comments

@sleberknight
Copy link
Member

Currently to get the result of the last failed attempt in a RetryException you need a cast:

var result = (Integer) retryException.getLastFailedAttempt();

To avoid this and make the API a bit nicer, add a method to RetryException to get the last failed Attempt with a specific type:

// result has inferred type of Integer
var result = retryException.getLastFailedAttempt(Integer.class);

// or define the type explicitly
Integer result = retryException.getLastFailedAttempt(Integer.class);

Of course, the caller must know the actual type or else a ClassCastException is thrown, but that's just Java...

@sleberknight sleberknight added the enhancement A request for change or improvement to an existing feature label Jun 5, 2024
@sleberknight sleberknight added this to the 2.0.4 milestone Jun 5, 2024
@sleberknight sleberknight self-assigned this Jun 5, 2024
@sleberknight
Copy link
Member Author

This was added as the "fix" to Should Attempt even have a type parameter?

sleberknight added a commit that referenced this issue Jun 6, 2024
* Add getLastFailedAttempt(Class<T> type) to RetryException
* Add reference to this method in API Note in getLastFailedAttempt()

Closes #224
sleberknight added a commit that referenced this issue Jun 6, 2024
* Add getLastFailedAttempt(Class<T> type) to RetryException
* Add reference to this method in API Note in getLastFailedAttempt()

Details:

Instead of just casting, check that the Attempt either:

1. does not contain a result (it contains an Exception)
2. contains a result that can be assigned to the specified type

If either the above conditions is true, then a cast is safe.

Define the function to throw IlegalStateException with a
descriptive message instead of just letting th JVM throw
a ClassCastException with no message.

Closes #224
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A request for change or improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant