I was trying to debug an issue where we were responding with 500 due to an unhandled exception while calling one of the downstream services. The exception which we were getting while calling the downstream service was:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://test-1e2e.api.com/v1/flowable/channels": test-1e2e.api.com: nodename nor servname provided, or not known; nested exception is java.net.UnknownHostException: test-1e2e.api.com: nodename nor servname provided, or not known
I understood we were getting an unknown host exception, but according to my understanding the same should have been caught for HttpClientErrorException. Our code was such:
try {
StopWatch stopWatch = StopWatch.createStarted();
ResponseEntity<Channels> responseEntity = restTemplate.exchange(testChannelsUrl, HttpMethod.POST, requestHttpEntity, ABC.class);
stopWatch.stop();
} catch (UnknownHttpStatusCodeException | HttpClientErrorException ex) {
throw new ServiceException(transactionContext.getTid(), ex, ex.getMessage());
}
But the exception is thrown was different: ResourceAccessException
When I tried to debug further to understand the behavior, we found that:
Let say the correct downstream API URL is: https://test.api.com/v1/flowable/channels
If I have some error in the host, let say https://test.123.api.com/v1/flowable/channels, it will throw ResourceAccessException without any specific HTTP status code.
If I have some error in the path, let say https://test.api.com/v1/flowable/**test-channels**, it will throw HttpClientErrorException with 404.
Why is such, what is the difference between the two from the socket connection point of view?
PS: Also, since we did not get any status code in ResourceAccessException - should it not be UnknownHttpStatusCodeException.
Please help me understand this concept.
Thanks in advance.