From 2f04ffad4b747f1b61b3c9ecc46d6e8b2cba2cca Mon Sep 17 00:00:00 2001 From: Mike Cowan Date: Thu, 3 Nov 2016 17:30:40 +0000 Subject: [PATCH] Add Observable support for option to raise HystrixRuntimeException --- .../aop/aspectj/HystrixCommandAspect.java | 16 +++++++---- ...asicDefaultRaiseHystrixExceptionsTest.java | 28 ++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect.java b/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect.java index 3f1347fc2..f7d6c0627 100644 --- a/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect.java +++ b/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect.java @@ -104,15 +104,12 @@ public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinP } catch (HystrixBadRequestException e) { throw e.getCause(); } catch (HystrixRuntimeException e) { - if (metaHolder.raiseHystrixExceptionsContains(HystrixException.RUNTIME_EXCEPTION)) { - throw e; - } - throw getCause(e); + throw hystrixRuntimeExceptionToThrowable(metaHolder, e); } return result; } - private Observable executeObservable(HystrixInvokable invokable, ExecutionType executionType, MetaHolder metaHolder) { + private Observable executeObservable(HystrixInvokable invokable, ExecutionType executionType, final MetaHolder metaHolder) { return ((Observable) CommandExecutor.execute(invokable, executionType, metaHolder)) .onErrorResumeNext(new Func1() { @Override @@ -121,13 +118,20 @@ public Observable call(Throwable throwable) { return Observable.error(throwable.getCause()); } else if (throwable instanceof HystrixRuntimeException) { HystrixRuntimeException hystrixRuntimeException = (HystrixRuntimeException) throwable; - return Observable.error(getCause(hystrixRuntimeException)); + return Observable.error(hystrixRuntimeExceptionToThrowable(metaHolder, hystrixRuntimeException)); } return Observable.error(throwable); } }); } + private Throwable hystrixRuntimeExceptionToThrowable(MetaHolder metaHolder, HystrixRuntimeException e) { + if (metaHolder.raiseHystrixExceptionsContains(HystrixException.RUNTIME_EXCEPTION)) { + return e; + } + return getCause(e); + } + private Throwable getCause(HystrixRuntimeException e) { if (e.getFailureType() != HystrixRuntimeException.FailureType.COMMAND_EXCEPTION) { return e; diff --git a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/error/BasicDefaultRaiseHystrixExceptionsTest.java b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/error/BasicDefaultRaiseHystrixExceptionsTest.java index d5a6951d0..95c862eae 100644 --- a/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/error/BasicDefaultRaiseHystrixExceptionsTest.java +++ b/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/error/BasicDefaultRaiseHystrixExceptionsTest.java @@ -4,11 +4,15 @@ import org.junit.Ignore; import org.junit.Test; +import com.netflix.hystrix.Hystrix; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixException; import com.netflix.hystrix.exception.HystrixRuntimeException; +import rx.Observable; +import rx.observers.TestSubscriber; + /** * Created by Mike Cowan */ @@ -56,10 +60,32 @@ public void testFallbackCommandOverridesDefaultIgnoreExceptions_nonIgnoreExcepti service.commandWithFallbackOverridesDefaultIgnoreExceptions(BadRequestException.class); } + @Test(expected = HystrixRuntimeException.class) + public void testRaiseHystrixRuntimeException() { + service.commandShouldRaiseHystrixRuntimeException(); + } + + @Test + public void testObservableRaiseHystrixRuntimeException() { + TestSubscriber testSubscriber = new TestSubscriber(); + service.observableCommandShouldRaiseHystrixRuntimeException().subscribe(testSubscriber); + testSubscriber.assertError(HystrixRuntimeException.class); + } + @DefaultProperties(ignoreExceptions = BadRequestException.class, raiseHystrixExceptions = {HystrixException.RUNTIME_EXCEPTION}) public static class Service { @HystrixCommand - public Object commandInheritsDefaultIgnoreExceptions() throws BadRequestException { + public Object commandShouldRaiseHystrixRuntimeException() throws SpecificException { + throw new SpecificException("from 'commandShouldRaiseHystrixRuntimeException'"); + } + + @HystrixCommand + public Observable observableCommandShouldRaiseHystrixRuntimeException() throws SpecificException { + return Observable.error(new SpecificException("from 'observableCommandShouldRaiseHystrixRuntimeException'")); + } + + @HystrixCommand + public Object commandInheritsDefaultIgnoreExceptions() throws BadRequestException { // this exception will be ignored (wrapped in HystrixBadRequestException) because specified in default ignore exceptions throw new BadRequestException("from 'commandInheritsIgnoreExceptionsFromDefault'"); }