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.enabled = true.
Please help with this. I want to propagate the exception as it is. I do not want it to be wrapped.
Exception is
: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.netflix.hystrix.exception.HystrixRuntimeException: ProductFeignService#validateProducts(product) failed and fallback failed.] with root cause
Comment From: OlgaMaciaszek
@shivasantosh Please learn how to properly format code and logs.
Comment From: OlgaMaciaszek
Please provide a minimal, complete, verifiable example that reproduces the issue.
Comment From: shivasantosh
@OlgaMaciaszek
Below are the required details.
Here is my git public link which has two services
https://github.com/shivasantosh/feignproject
Order service is calling Product service using feign client.
Steps for replication:
1. Run the two spring boot projects.
2. Call the POST end point "http://localhost:
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ProductFeignServiceFallBack implements ProductFeignService{
private static final Logger LOGGER = LogManager.getLogger(ProductFeignServiceFallBack.class);
private final Throwable cause;
public ProductFeignServiceFallBack(Throwable cause) {
this.cause = cause;
}
@Override
public ResponseEntity<String> validateProduct(long productId) {
// my code for updating errors in my DB
//throw exception to the client
LOGGER.error(cause.getMessage());
LOGGER.info("thrown exception from upstream so entered fall back");
throw new ProductNotFoundException("PRODUCT_NOT_FOUND", "PRODUCT_NOT_FOUND");
}
}
- But I see in the Controller advice it is entering into HystrixRuntimeException block. I want it to be entered into productNotFoundException block. ProductNotFoundException is wrapped in HystrixRuntimeException. I want the business exception to be thrown as it is instead of wrapping it in HystrixRuntimeException. Because in my service class I donot want to catch with HystrixRuntimeException.
```java @ControllerAdvice public class OrderServiceExceptionHandler {
/*Exception has to enter this block*/
@ExceptionHandler(ProductNotFoundException.class)
public @ResponseBody ResponseEntity<ErrorDetails> productNotFoundException(ProductNotFoundException ex) {
ErrorDetails errorDetails = new ErrorDetails(ex.getErrorMessage(), ex.getErrorCode());
return new ResponseEntity<>(errorDetails, HttpStatus.EXPECTATION_FAILED);
}
/*Should not enter this block*/
@ExceptionHandler(HystrixRuntimeException.class)
public @ResponseBody ResponseEntity<String> hystrix(HystrixRuntimeException ex) {
return new ResponseEntity<>("Hystrix bloc", HttpStatus.CONFLICT);
}
} ```
Comment From: OlgaMaciaszek
@shivasantosh I have verified it and this, indeed, happens. However, first of all, I would not consider it a bug: any exception occurring within a feign call is wrapped with a FeignException as a design decision. Secondly, I would definitely not consider throwing an exception from the fallback method a good practice, either - the fallback method should provide a default replacement result when the actual result cannot be retrieved and would suggest you modify this approach. Lastly, this is being handled by the core Feign library and not the Spring Cloud OpenFeign library, so any discussion should take place within that repo.