Skip to content

Commit

Permalink
iss993: propagate original exception instead of HystrixRuntimeException
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgcodevil committed Jul 2, 2016
1 parent c226fdd commit 155d4e9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import com.netflix.hystrix.contrib.javanica.command.ExecutionType;
import com.netflix.hystrix.contrib.javanica.command.HystrixCommandFactory;
import com.netflix.hystrix.contrib.javanica.command.MetaHolder;
import com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException;
import com.netflix.hystrix.contrib.javanica.utils.AopUtils;
import com.netflix.hystrix.contrib.javanica.utils.FallbackMethod;
import com.netflix.hystrix.contrib.javanica.utils.MethodProvider;
import com.netflix.hystrix.exception.HystrixBadRequestException;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.aspectj.lang.JoinPoint;
Expand Down Expand Up @@ -93,10 +95,21 @@ public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinP
result = CommandExecutor.execute(invokable, executionType, metaHolder);
} catch (HystrixBadRequestException e) {
throw e.getCause();
} catch (HystrixRuntimeException e) {
throw getCauseOrDefault(e, e);
}
return result;
}

private Throwable getCauseOrDefault(RuntimeException e, RuntimeException defaultException) {
if (e.getCause() == null) return defaultException;
if (e.getCause() instanceof CommandActionExecutionException) {
CommandActionExecutionException commandActionExecutionException = (CommandActionExecutionException) e.getCause();
return Optional.fromNullable(commandActionExecutionException.getCause()).or(defaultException);
}
return e.getCause();
}

/**
* A factory to create MetaHolder depending on {@link HystrixPointcutType}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,13 @@ Object process(Action action) throws Exception {
if (isIgnorable(cause)) {
throw new HystrixBadRequestException(cause.getMessage(), cause);
}
if (cause instanceof Exception) {
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Exception) {
throw (Exception) cause;
} else {
throw Throwables.propagate(cause);
// instance of Throwable
throw new CommandActionExecutionException(cause);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testCommandOverridesDefaultIgnoreExceptions() {
service.commandOverridesDefaultIgnoreExceptions(SpecificException.class);
}

@Test(expected = HystrixRuntimeException.class)
@Test(expected = BadRequestException.class)
public void testCommandOverridesDefaultIgnoreExceptions_nonIgnoreExceptionShouldBePropagated() {
// method throws BadRequestException that isn't ignored
service.commandOverridesDefaultIgnoreExceptions(BadRequestException.class);
Expand All @@ -51,7 +51,7 @@ public void testFallbackCommandOverridesDefaultIgnoreExceptions() {
service.commandWithFallbackOverridesDefaultIgnoreExceptions(SpecificException.class);
}

@Test(expected = HystrixRuntimeException.class)
@Test(expected = SpecificException.class)
public void testFallbackCommandOverridesDefaultIgnoreExceptions_nonIgnoreExceptionShouldBePropagated() {
service.commandWithFallbackOverridesDefaultIgnoreExceptions(BadRequestException.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testActivateUser() throws NotFoundException, ActivationException {
}
}

@Test(expected = HystrixRuntimeException.class)
@Test(expected = OperationException.class)
public void testBlockUser() throws NotFoundException, ActivationException, OperationException {
try {
userService.blockUser("1"); // this method always throws ActivationException
Expand All @@ -123,6 +123,11 @@ public void testBlockUser() throws NotFoundException, ActivationException, Opera
}
}

@Test(expected = NotFoundException.class)
public void testPropagateCauseException() throws NotFoundException {
userService.deleteUser("");
}

public static class UserService {

private FailoverService failoverService;
Expand All @@ -131,6 +136,11 @@ public void setFailoverService(FailoverService failoverService) {
this.failoverService = failoverService;
}

@HystrixCommand
public Object deleteUser(String id) throws NotFoundException {
throw new NotFoundException("");
}

@HystrixCommand(
commandKey = COMMAND_KEY,
ignoreExceptions = {
Expand Down

0 comments on commit 155d4e9

Please sign in to comment.