I have tried using RestClient
as the backend for @HttpExchange
interfaces, and it throws a RestClientResponseException
(a subclass of RestClientException
) when an exception occurs.
When I use WebClient
as the backend for @HttpExchange
interfaces, it throws a WebClientResponseException
(a subclass of WebClientException
) when an exception occurs.
This problem can become particularly challenging in scenarios where logic needs to be handled based on an exception state. For example, the following code will not work as expected if I change the backend from RestClient
to WebClient
.
public Foo getFooById(String id) {
// FooApi is a @HttpExchange interface
FooApi api = ...
try {
return api.get(id);
} catch (HttpClientErrorException.NotFound e) {
return null;
}
}
So, my point is, why not unify the RestClient
and WebClient
exceptions in the spring-web
module?
Comment From: rstoyanchev
Thanks for the thought. We've discussed this but decided against it. RestClient
and WebClient
are already abstractions over HTTP client libraries, and we don't want to introduce another exception hierarchy. There is also no good option to introduce a common hieararchy in a backwards compatible way. Generally, we don't expect frequent switching the clients, but when you do you'll need to switch the exceptions too as part of the migration.