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

"sneaky throw" original Exception instead of FailsafeException when retries exceeded #331

Closed
RG9 opened this issue Feb 18, 2022 · 3 comments

Comments

@RG9
Copy link

RG9 commented Feb 18, 2022

test:

    @Test
    void shouldRethrowOriginalException() {
        assertThatThrownBy(() -> Failsafe.with(RetryPolicy.ofDefaults())
            .run(() -> {
                throw new IOException();
            }))
                .isInstanceOf(FailsafeException.class);
    }

my current workaround:

 try {
      Failsafe.with(retryPolicy).run(taskThrowingIoException);
  } catch (FailsafeException e) {
      throw sneakyThrow(e.getCause());
  }
            
 @SuppressWarnings("unchecked")
  private static <T extends Throwable, R extends RuntimeException> R sneakyThrow(Throwable t) throws T {
      throw (T) t;
  }

Is there any better way to deal with it?

@jhalterman
Copy link
Member

jhalterman commented Feb 19, 2022

I think the only improvement would be for Failsafe to add an option to support sneaky throws, ex:

Failsafe.with(policy).withSneakyThrows().run(this::doSomething);

I've always wondered if this would be requested, but surprisingly it hasn't so far. Is unwrapping checked exceptions just inconvenient for you?

@RG9
Copy link
Author

RG9 commented Feb 21, 2022

Thanks for answer.
I think I just tried to implement an overly generic solution.
After narrowing down the scope, unwrapping is also fine 👍
Besides, from my understanding, checked exceptions were designed to support recover from error, but actually this is where Failsafe takes the baton.

@jhalterman
Copy link
Member

Good to hear. I'll close this issue for now then.

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

No branches or pull requests

2 participants