Netflix / Hystrix

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.
23.98k stars 4.7k forks source link

Donot wrap custom exception to HystrixRunTimeException in Feign Client Fall back methods #1997

Open shivasantosh opened 4 years ago

shivasantosh commented 4 years ago

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.

ZengRed commented 3 years ago

I'm bothering with this problem too. In fact, throwing a custom exception sometime reduce complexity, think of a @service invoke a FeignClient's interface which returns a DTO, when interface curcuit break then go to fallback method then throws a custom exception, IF this custom exception is NOT turn into HystrixRunTimeException , then @ExceptionHandler will handle this custom exception(this exception contains msg provided where it throws). But in fact a custom exception is totally turn into HystrixRunTimeException without any "cause" exception info that can be found--this is not so reasonable I think.

shivasantosh commented 3 years ago

yeah @ZengRed I guess this should be to feign core may be. I'll try to paste the same info there

cmyker commented 3 years ago

Solution is quite easy - just extend your exception from HystrixBadRequestException, details here https://netflix.github.io/Hystrix/javadoc/com/netflix/hystrix/exception/HystrixBadRequestException.html

shivasantosh commented 3 years ago

it's not working we have checked even extending. It works with plain Hystrix But we need fall back mechanism for feign where feign internally uses Hystrix. @cmyker

cmyker commented 3 years ago

Works for me, in similar manner described here https://stackoverflow.com/a/58414584/1079659

shivasantosh commented 3 years ago

Let me explain it clearly. @cmyker

image

In the above image the application is throwing HystrixRuntimeException where we should unwrap it.

But I donot want it to be wrapped because as soon as I throw ProductNotFoundException. In business logic it should catch with ProductNotFoundException. But in the hystrix case we should catch with HystrixRuntimeException I was unable to differentiate exception till I unwrap it.

In my case there is different logics needs to be done for different exceptions. but if it is only HystrixRuntimeException till I unwrap it I cannot differentiate my logic

In the business layer I should not see HystrixRuntimeException. I need ProductNotFoundException directly in catch block