Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Apache HttpClient execute throw an IOException on ALL HTTP 5XX errors?

The Apache HttpClient docs for the execute(HttpHost target, HttpRequest request) method says:

IOException - in case of a problem or the connection was aborted

If I catch the IOException, will this catch ALL Server 5xx Errors?

try {
  response = client.execute(httpHost, request);
} catch (IOException e) {
  // throw custom Exception
} finally {
  // close response and client
}

The reason I'm asking is that after this logic somewhere else down the line we're doing something like the following:

if (response.getStatusLine().getStatusCode() >= 500) {
  // Could we ever reach this point after the code above?
}
like image 943
Diyarbakir Avatar asked Sep 07 '15 09:09

Diyarbakir


1 Answers

No, HttpClient will not throw an IOException for any 500/5xx response.

An IOException occurs only when the low-level connection failed (eg. invalid hostname, no server listening) or the TCP pipe was abnormally broken (eg. internet connection was lost).

An 'HTTP 500' is a server response - a valid server response - to indicate an error condition. It has a status code, headers, and body, which is everything a 200 response has.

The documentation says the return value is "the [final] response to the request"; this is true regardless of the status code as long as the server was able to return a valid response.

like image 173
user2864740 Avatar answered Oct 30 '22 09:10

user2864740