Skip to content

Commit

Permalink
Fixes Netflix#1458 : it is now possible to implements "ExceptionNotWr…
Browse files Browse the repository at this point in the history
…appedByHystrix" to get back the original exception and not an HystrixBadRequestException
  • Loading branch information
mNantern committed Mar 19, 2017
1 parent 710a537 commit 82a6610
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1561,9 +1561,15 @@ protected Throwable decomposeException(Exception e) {
return (IllegalStateException) e;
}
if (e instanceof HystrixBadRequestException) {
if (shouldNotBeWrapped(e.getCause())) {
return e.getCause();
}
return (HystrixBadRequestException) e;
}
if (e.getCause() instanceof HystrixBadRequestException) {
if(shouldNotBeWrapped(e.getCause().getCause())) {
return e.getCause().getCause();
}
return (HystrixBadRequestException) e.getCause();
}
if (e instanceof HystrixRuntimeException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public interface AbstractTestHystrixCommand<R> extends HystrixObservable<R>, InspectableBuilder {

enum ExecutionResult {
SUCCESS, FAILURE, ASYNC_FAILURE, HYSTRIX_FAILURE, NOT_WRAPPED_FAILURE, ASYNC_HYSTRIX_FAILURE, RECOVERABLE_ERROR, ASYNC_RECOVERABLE_ERROR, UNRECOVERABLE_ERROR, ASYNC_UNRECOVERABLE_ERROR, BAD_REQUEST, ASYNC_BAD_REQUEST, MULTIPLE_EMITS_THEN_SUCCESS, MULTIPLE_EMITS_THEN_FAILURE, NO_EMITS_THEN_SUCCESS
SUCCESS, FAILURE, ASYNC_FAILURE, HYSTRIX_FAILURE, NOT_WRAPPED_FAILURE, ASYNC_HYSTRIX_FAILURE, RECOVERABLE_ERROR, ASYNC_RECOVERABLE_ERROR, UNRECOVERABLE_ERROR, ASYNC_UNRECOVERABLE_ERROR, BAD_REQUEST, ASYNC_BAD_REQUEST, BAD_REQUEST_NOT_WRAPPED, MULTIPLE_EMITS_THEN_SUCCESS, MULTIPLE_EMITS_THEN_FAILURE, NO_EMITS_THEN_SUCCESS
}

enum FallbackResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ public void testNotWrappedExceptionWithNoFallback() {
assertSaneHystrixRequestLog(1);
}

/**
* Test a command execution that throws an exception that should not be wrapped.
*/
@Test
public void testNotWrappedBadRequestWithNoFallback() {
TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.BAD_REQUEST_NOT_WRAPPED, AbstractTestHystrixCommand.FallbackResult.UNIMPLEMENTED);
try {
command.execute();
fail("we shouldn't get here");
} catch (HystrixRuntimeException e) {
e.printStackTrace();
fail("we shouldn't get a HystrixRuntimeException");
} catch (RuntimeException e) {
assertTrue(e instanceof NotWrappedByHystrixTestRuntimeException);
}

assertTrue(command.getExecutionTimeInMilliseconds() > -1);
assertTrue(command.getEventCounts().contains(HystrixEventType.BAD_REQUEST));
assertCommandExecutionEvents(command, HystrixEventType.BAD_REQUEST);
assertNotNull(command.getExecutionException());
assertTrue(command.getExecutionException() instanceof HystrixBadRequestException);
assertTrue(command.getExecutionException().getCause() instanceof NotWrappedByHystrixTestRuntimeException);
assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount());
assertSaneHystrixRequestLog(1);
}


/**
* Test a command execution that fails but has a fallback.
*/
Expand Down Expand Up @@ -4908,6 +4935,8 @@ protected Integer run() throws Exception {
throw new StackOverflowError("Unrecoverable Error for TestHystrixCommand");
} else if (executionResult == AbstractTestHystrixCommand.ExecutionResult.BAD_REQUEST) {
throw new HystrixBadRequestException("Execution BadRequestException for TestHystrixCommand");
} else if (executionResult == AbstractTestHystrixCommand.ExecutionResult.BAD_REQUEST_NOT_WRAPPED) {
throw new HystrixBadRequestException("Execution BadRequestException for TestHystrixCommand", new NotWrappedByHystrixTestRuntimeException());
} else {
throw new RuntimeException("You passed in a executionResult enum that can't be represented in HystrixCommand: " + executionResult);
}
Expand Down

0 comments on commit 82a6610

Please sign in to comment.