-
Couldn't load subscription status.
- Fork 38.8k
Description
I am currently migrating an old declarative Feign client to Spring Web. The old client uses checked exceptions, and I would like to keep that behavior (it is very useful to be able to immediately know what kind of problems you might encounter when invoking the client, IDE suggesting which exceptions you need to catch etc.).
In Feign my client looked like this:
public interface SomeClientInterface {
@RequestLine("GET /some/path")
ReturnType someMethod() throws CheckedException1, CheckedException2;
}Feign's ErrorDecoder interface has the method Exception decode(String methodKey, Response response), which allows to decode server response into any checked exception.
The same client in Spring Web looks like this:
@HttpExchange
public interface SomeClientInterface {
@GetExchange("/some/path")
ReturnType someMethod() throws CheckedException1, CheckedException2;
}Hovewer, Spring's ResponseErrorHandler only allows to throw IOException and unchecked exceptions (void handleError(ClientHttpResponse response) throws IOException). The client is created using RestClient.Builder with custom defaultStatusHandler, and HttpServiceProxyFactory.
I've been able to work around the issue by using lombok's @SneakyThrows. But I think it would make much more sense to allow custom checked exceptions in ResponseErrorHandler, possibly by changing handleError to be able to throw Exception, or by giving it return type Exception (or Optional<Exception>) and using the returned value as an exception thrown by invoking the client.