I want to handle any exception from feign client, even if service is not available. However I can not catch them using try/catch. This is my feign client:
@FeignClient(name = "api-service", url ="localhost:8888")
public interface ClientApi extends SomeApi {
}
Where api is:
@Path("/")
public interface SomeApi {
@GET
@Path("test")
String getValueFromApi();
}
Usage of client with try/catch:
@Slf4j
@Service
@AllArgsConstructor
public class SampleController implements SomeApi {
@Autowired
private final ClientApi clientApi;
@Override
public String getValueFromApi() {
try {
return clientApi.getValueFromApi();
} catch (Throwable e) {
log.error("CAN'T CATCH");
return "";
}
}
}
Dependencies are in versions:
- spring-boot 2.2.2.RELEASE
- spring-cloud Hoxton.SR1
Code should work according to How to manage Feign errors?.
I received few long stack traces among them exceptions are :
- Caused by: java.net.ConnectException: Connection refused (Connection refused)
- Caused by: feign.RetryableException: Connection refused (Connection refused) executing GET http://localhost:8888/test
- Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: ClientApi#getValueFromApi() failed and no fallback available.
How to properly catch Feign exeptions, even if client service (in this case localhost:8888) is not available?
Ps. When feign client service is available it works, ok. I am just focused on the exceptions aspect.