Skip to content

Commit

Permalink
GH-405: Re-throw as is after recoverer
Browse files Browse the repository at this point in the history
Fixes: #405

The unchecked exception is wrapped into `UndeclaredThrowableException`
if `recoverer` is in use; it is not otherwise

* Fix `RetryTemplate` to catch `UndeclaredThrowableException` from `recoveryCallback.recover()`
and re-throw its `cause` instead to re-align behavior when no recoverer provided
  • Loading branch information
artembilan committed Dec 11, 2023
1 parent e029dfd commit dbfb3c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.retry.support;

import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -545,9 +546,14 @@ protected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, Retry
boolean doRecover = !Boolean.TRUE.equals(context.getAttribute(RetryContext.NO_RECOVERY));
if (recoveryCallback != null) {
if (doRecover) {
T recovered = recoveryCallback.recover(context);
context.setAttribute(RetryContext.RECOVERED, true);
return recovered;
try {
T recovered = recoveryCallback.recover(context);
context.setAttribute(RetryContext.RECOVERED, true);
return recovered;
}
catch (UndeclaredThrowableException undeclaredThrowableException) {
throw wrapIfNecessary(undeclaredThrowableException.getUndeclaredThrowable());
}
}
else {
logger.debug("Retry exhausted and recovery disabled for this throwable");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ public void excludesOnly() {
service.setExceptionToThrow(new IllegalArgumentException());
service.service();
assertThat(service.getCount()).isEqualTo(3);

assertThatExceptionOfType(InstantiationException.class).isThrownBy(service::reThrowAsIs).withMessage("noRetry");

context.close();
}

Expand Down Expand Up @@ -750,6 +753,16 @@ public void setExceptionToThrow(RuntimeException exceptionToThrow) {
this.exceptionToThrow = exceptionToThrow;
}

@Retryable(noRetryFor = InstantiationException.class, recover = "noRetryRecovery")
public void reThrowAsIs() throws InstantiationException {
throw new InstantiationException("noRetry");
}

@Recover
public void noRetryRecovery(Throwable ex) throws Throwable {
throw ex;
}

}

protected static class StatefulService {
Expand Down

0 comments on commit dbfb3c7

Please sign in to comment.