Reputation: 63
Fallback service is wrapping ProductNotFoundException into HystrixRunTimeException. I want to propagate custom exception as it is instead of wrapping into HystrixRunTimeException. Below is the code snippet for reference:
@FeignClient(name = "service1", fallback = FallBackService1.class )
public interface FeignService1{
String validateProducts(Product product);
}
class FallBackService1 implements FeignService1{
@override
public String validateProducts(Product product){
throw new ProductNotFoundException("P119","Product not found");
}
}
I have enabled feign.hystrix.enable = true.
Please help with this. I want to propagate the exception as it is. I do not want it to be wrapped.
Upvotes: 2
Views: 2090
Reputation: 146
It looks impossible to tell hystrix to not wrap your custom exception in the fallback method.
What you can do is to create your custom ErrorDecoder and throw your custom exception from there. In this case, your custom exception should implement com.netflix.hystrix.exception.ExceptionNotWrappedByHystrix
otherwise hystrix will wrap it into HystrixRuntimeException
.
Simple example.
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Bean;
public class FeignClient1Config {
@Bean
public ErrorDecoder customErrorDecoder() {
return (methodKey, response) -> {
throw new ProductNotFoundException("P119","Product not found for request " + response.request());
};
}
}
@FeignClient(name = "service1", configuration = FeignClient1Config.class)
public interface FeignService1{
String validateProducts(Product product);
}
The drawback of this implementation is that all methods in the FeignService1 will be processed by the same error decoder.
To overcome it you can create your own fallbackFactory and add it as a property to the @FeignClient(fallbackFactory = ...)
Or the solution I like the most is this
And do not forget your exception to implement ExceptionNotWrappedByHystrix class.
Upvotes: 1