Unable to access the cause in FallbackFactory
See original GitHub issueI have the HystrixFeign client and I am trying to get the cause/exception in my fallback implementation, because I would really like to know the reason for fallback so I can fix the issue why the service call has failed. But the below implementations are not getting me the cause. This works just fine and the fallback is getting called all the time. But I have no clue why. I am new to Feign and Hystrix. My application is written in java 1.6 years back and this is kind of enhancement call. So I cant go for any lambda expressions.
I have the client interface defined like below
public interface MyServiceFeignClient {
@RequestLine("POST /myService/order")
@Headers("Content-Type:application/vnd.org.company.domain.order+json;version=1.0")
ServiceResponse sendOrder(String content);
}
My FeignClientFacory is like below
public class FeignClientFactory {
private static final Logger LOG = LoggerFactory.getLogger(FeignClientFactory.class);
private String serviceUrl;
public FeignClientFactory(final String serviceUrl) {
this.serviceUrl = serviceUrl;
}
public MyServiceFeignClient newInstance() {
return HystrixFeign.builder()
.decoder(new GsonDecoder())
.target(MyServiceFeignClient.class, serviceUrl, new ClientFallbackFactory());
}
static class ClientFallbackFactory implements MyServiceFeignClient, FallbackFactory<ClientFallbackFactory> {
final Throwable cause;
public ClientFallbackFactory() {
this(null);
}
ClientFallbackFactory(Throwable cause) {
this.cause = cause;
}
// note that this method is not getting called at all
@Override
public ClientFallbackFactory create(Throwable cause) {
if (cause != null) {
String errMessage = StringUtils.isNotBlank(cause.getMessage()) ? cause.getMessage() : "unknown error occured";
// I don't see this log statement
LOG.debug("Client fallback called for the cause : {}", errMessage);
}
return new ClientFallbackFactory(cause);
}
// everytime this method is called as fallback and the cause is just NULL
@Override
public ServiceResponse sendOrder(String content) {
LOG.debug("service client api fallback called");
ServiceResponse response = new ServiceResponse();
String errMessage = (cause == null ? "service client api fallback called" : cause.getMessage());
response.setErrorMessage(errMessage);
response.setResultStatus("WARN");
return response;
}
}
}
My test case is like below.
@Test
public void testSendOrderToPaymentRiskService() {
String order = loadFile("/order.json");
ServiceResponse response = client.sendOrder(order);
// status is always WARN - so fallback is getting called everytime.
assertThat(response.getResultStatus()).isEqualTo("SUCCESS");
assertThat(response.getTransactionId()).isNotNull();
// getting the error message "service client api fallback called"
assertThat(response.getErrorMessage).isNull();
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Issue in getting cause in HystrixFeign client fallback
I am new to Feign and Hystrix. My application is written in java 1.6 years back and this is kind of enhancement call....
Read more >Fallbacks with Spring Cloud Feign - Arnold Galovics
With the regular fallback, you can't access the underlying exception that triggered the fallback however with the fallback factory, you can.
Read more >Quick Understanding for Fallback in Spring Cloud Feign
This solution is quite straight-forward but not able to capture the http status code and cause that made the fallback as they are...
Read more >7. Declarative REST Client: Feign - Spring Cloud
If one needs access to the cause that made the fallback trigger, one can use the fallbackFactory attribute inside @FeignClient .
Read more >A Guide to Spring Cloud Netflix - Hystrix - Baeldung
If there is such a failure, it will open the circuit and forward the call to a fallback method. The library will tolerate...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So taking the test case and start modifying it one by one helped me fix the issue. FallbackFactory is getting me the cause. Thank you @adriancole and @spencergibb
You’ll need to cast it as a
FallbackFactory
to select the appropriate method since your class is both.