Skip to content

Commit

Permalink
Adding option to raise HystrixRuntimeException instead of cause
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Cowan committed Oct 20, 2016
1 parent 4e08f1a commit 8d8a8b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
HystrixProperty[] threadPoolProperties() default {};

/**
* Defines exceptions which should be ignored and wrapped to throw in HystrixBadRequestException.
* Defines exceptions which should be ignored.
* Optionally these can be wrapped in a HystrixRuntimeException if raiseHystrixExceptions is set to true.
*
* @return exceptions to ignore
*/
Expand All @@ -113,5 +114,11 @@
* @return observable execution mode
*/
ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;

/**
* When set to true, any exceptions that are not ignored are wrapped in HystrixRuntimeException.
* @return true to raise HystrixRuntimeException instead of their cause.
*/
boolean raiseHystrixRuntimeExceptions() default false;
}

Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinP
} catch (HystrixBadRequestException e) {
throw e.getCause();
} catch (HystrixRuntimeException e) {
if (metaHolder.getRaiseHystrixRuntimeExceptions()) {
throw e;
}
throw getCause(e);
}
return result;
Expand Down Expand Up @@ -253,6 +256,7 @@ public MetaHolder create(Object proxy, Method method, Object obj, Object[] args,
return builder.defaultCommandKey(method.getName())
.hystrixCommand(hystrixCommand)
.observableExecutionMode(hystrixCommand.observableExecutionMode())
.raiseHystrixRuntimeExceptions(hystrixCommand.raiseHystrixRuntimeExceptions())
.executionType(executionType)
.observable(ExecutionType.OBSERVABLE == executionType)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public final class MetaHolder {
private final JoinPoint joinPoint;
private final boolean observable;
private final ObservableExecutionMode observableExecutionMode;
private final boolean raiseHystrixRuntimeExceptions;

private static final Function identityFun = new Function<Object, Object>() {
@Nullable
Expand Down Expand Up @@ -101,6 +102,7 @@ private MetaHolder(Builder builder) {
this.extendedParentFallback = builder.extendedParentFallback;
this.observable = builder.observable;
this.observableExecutionMode = builder.observableExecutionMode;
this.raiseHystrixRuntimeExceptions = builder.raiseHystrixRuntimeExceptions;
}

public static Builder builder() {
Expand Down Expand Up @@ -299,6 +301,10 @@ public ObservableExecutionMode getObservableExecutionMode() {
return observableExecutionMode;
}

public boolean getRaiseHystrixRuntimeExceptions() {
return raiseHystrixRuntimeExceptions;
}

private String get(String key, String defaultKey) {
return StringUtils.isNotBlank(key) ? key : defaultKey;
}
Expand Down Expand Up @@ -353,6 +359,7 @@ public static final class Builder {
private boolean observable;
private JoinPoint joinPoint;
private ObservableExecutionMode observableExecutionMode;
private boolean raiseHystrixRuntimeExceptions;

public Builder hystrixCollapser(HystrixCollapser hystrixCollapser) {
this.hystrixCollapser = hystrixCollapser;
Expand Down Expand Up @@ -474,6 +481,11 @@ public Builder observableExecutionMode(ObservableExecutionMode observableExecuti
return this;
}

public Builder raiseHystrixRuntimeExceptions(boolean raiseHystrixRuntimeExceptions) {
this.raiseHystrixRuntimeExceptions = raiseHystrixRuntimeExceptions;
return this;
}

public MetaHolder build() {
return new MetaHolder(this);
}
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 = BadRequestException.class)
@Test(expected = HystrixRuntimeException.class)
public void testCommandOverridesDefaultIgnoreExceptions_nonIgnoreExceptionShouldBePropagated() {
// method throws BadRequestException that isn't ignored
service.commandOverridesDefaultIgnoreExceptions(BadRequestException.class);
Expand Down Expand Up @@ -64,7 +64,7 @@ public Object commandInheritsDefaultIgnoreExceptions() throws BadRequestExceptio
throw new BadRequestException("from 'commandInheritsIgnoreExceptionsFromDefault'");
}

@HystrixCommand(ignoreExceptions = SpecificException.class)
@HystrixCommand(ignoreExceptions = SpecificException.class, raiseHystrixRuntimeExceptions = true)
public Object commandOverridesDefaultIgnoreExceptions(Class<? extends Throwable> errorType) throws BadRequestException, SpecificException {
if(errorType.equals(BadRequestException.class)){
// isn't ignored because command doesn't specify this exception type in 'ignoreExceptions'
Expand Down

0 comments on commit 8d8a8b3

Please sign in to comment.