Skip to content

Commit

Permalink
Add Observable support for option to raise HystrixRuntimeException
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Cowan committed Nov 3, 2016
1 parent 7598e9a commit 2f04ffa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Throwable, Observable>() {
@Override
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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<Void> testSubscriber = new TestSubscriber<Void>();
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<Void> 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'");
}
Expand Down

0 comments on commit 2f04ffa

Please sign in to comment.