Skip to content

Rethrow undeclared exceptions #261

@ttulka

Description

@ttulka

Not sure if this is a "works as designed" problem, but it seems to be useful to support this use-case.
So far, I was not able to make it work:

I need the retry mechanism to be triggered only for declared exceptions and let others be rethrown.

With this code:

@Retryable(include = {ToRetryException.class}, recover = "recoverRetrieveFromExternal")
public String retrieveFromExternal(String arg) {
    return externalService.retrieve(arg);
}
@Recover
private String recoverRetrieveFromExternal(Throwable t, String arg) {
    return "Recover: " + arg;
}

When an exception different from ToRetryException is thrown by externalService.retrieve(arg) the recover fallback is used.

With stateful = true, all exceptions including ToRetryException are rethrown.

Basically, I need this test suite to pass:

@Test
void ok() {
    when(externalService.retrieve(anyString())).thenReturn("TEST");
    assertThat(client.retrieveFromExternal("")).isEqualTo("TEST");
}
@Test
void rethrows_the_exception() {
    when(externalService.retrieve(anyString())).thenThrow(new RuntimeException());
    assertThrows(RuntimeException.class, () -> client.retrieveFromExternal(""));
}
@Test
void retries_and_recovers() {
    when(externalService.retrieve(anyString())).thenThrow(new ToRetryException());
    assertThat(client.retrieveFromExternal("test")).isEqualTo("Recover: test");
}

Is there a way to do that? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions