I have a feign client with this external call:
@RequestMapping(method = RequestMethod.GET, value = "GetResourceA", consumes = "application/json")
@Cacheable("ResourceA")
List<Stop> getResourceA() throws MyOwnException;
And in my application.yml
I have this setting:
hystrix:
command:
default:
execution.isolation.thread.timeoutInMilliseconds: 1000
fallback.enabled: false
Now if getResourceA times out, i.e. it takes more than one second to complete, I either get this:
com.netflix.hystrix.exception.HystrixRuntimeException: getResourceA timed-out and no fallback available
Or, if I define a fallback from which I throw my own exception, I get this:
com.netflix.hystrix.exception.HystrixRuntimeException: getResourceA timed-out and fallback failed.
Can I not throw my own exception from the fallback?
What if I wish to throw my own exception when the service is down? I would like to not have a fallback (because I have no reasonable value to return from the fallback), but instead throw my own error that I can catch and let the program resume. Can someone help me out with this?
Update after the answer from Ben:
So I tried the approach with catching HysterixRuntimeException and checking what caused it, but ended up with this ugly code:
try {
getResourceA();
} catch (HystrixRuntimeException e) {
if (e.getFailureType().name().equals("TIMEOUT")) {
throw new MyOwnException("Service timed out");
}
throw e;
}
All that to be able to throw MyOwnException on a timeout. Surely there must be another way?
Simply add a Throwable parameter to the fallback method and it will receive the exception which the original command produced.
Hystrix does not throw HystrixRuntimeException, and instead empty message error. Bookmark this question.
RuntimeException that is thrown when a HystrixCommand fails and does not have a fallback. HystrixTimeoutException. An exception representing an error where the provided execution method took longer than the Hystrix timeout.
Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
You should be able to get the exception you throw from your fallback by getting the cause of the HystrixRuntimeException
So, to handle your custom exception, you can do this:
try {
getResourceA();
} catch (HystrixRuntimeException e) {
if (e.getCause() instanceof MyException) {
handleException((MyException)e.getCause());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With