You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Oleg Kalnichevski (JIRA)" <ji...@apache.org> on 2014/03/17 12:23:43 UTC
[jira] [Commented] (HTTPCLIENT-1485) Wrong type of exception raised
because of hardcoded string-compare
[ https://issues.apache.org/jira/browse/HTTPCLIENT-1485?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13937693#comment-13937693 ]
Oleg Kalnichevski commented on HTTPCLIENT-1485:
-----------------------------------------------
Dominik
I am perfectly aware of implications of doing exception handling based on content of the error message rather than the type of exception. However, if you take a closer look at the original exception thrown by the Socket#connect method you could see its type is actually java.net.ConnectException instead of java.net.SocketTimeoutException as specified by the method contract
{noformat}
public void connect(SocketAddress endpoint,
int timeout)
throws IOException
Connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.
Parameters:
endpoint - the SocketAddress
timeout - the timeout value to be used in milliseconds.
Throws:
IOException - if an error occurs during the connection
SocketTimeoutException - if timeout expires before connecting
IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode
IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket
Since:
1.4
{noformat}
The ConnectException ought not be used to signal a connect timeout based on its description
{noformat}
public class ConnectException
extends SocketException
Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port).
Since:
JDK1.1
{noformat}
So, how else was I supposed to differentiate connection refused form connect timeout conditions? Message content scraping was absolutely the last resort.
If you know how this can be done with non English locate I would happily change current behavior of the HttpClientConnectionOperator.
Oleg
> Wrong type of exception raised because of hardcoded string-compare
> ------------------------------------------------------------------
>
> Key: HTTPCLIENT-1485
> URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1485
> Project: HttpComponents HttpClient
> Issue Type: Bug
> Components: HttpClient
> Affects Versions: 4.3.2, 4.3.3
> Reporter: Dominik Bennersdorfer
>
> In the lines org.apache.http.impl.conn.HttpClientConnectionOperator.java:134-143 we found that the message of the ConnectException is compared to the hardcoded string "Connection timed out".
> This can causes wrong break down to the http-client own Exception-types on Systems with other languages.
> } catch (final ConnectException ex) {
> if (last) {
> final String msg = ex.getMessage();
> if ("Connection timed out".equals(msg)) {
> throw new ConnectTimeoutException(ex, host, addresses);
> } else {
> throw new HttpHostConnectException(ex, host, addresses);
> }
> }
> }
> To illustrate the Problem see the following Example:
> On our server the locale is set to "de_AT.ISO-8859-15@euro"
> with a simple wget i get this (see the message in last line)
> root@MRW-MJA-vml1:/tmp# wget http://www.google.at:81
> --2014-03-14 12:46:44-- http://www.google.at:81/
> Auflösen des Hostnamen www.google.at... 173.194.39.183
> Verbindungsaufbau zu www.google.at|173.194.39.183|:81... fehlgeschlagen: Die Wartezeit für die Verbindung ist abgelaufen.
> after changing the locale to "en_US.ISO-8859-15" i get the following:
> root@MRW-MJA-vml1:/tmp# wget http://www.google.at:81
> --2014-03-14 12:43:33-- http://www.google.at:81/
> Resolving www.google.at... 173.194.32.223
> Connecting to www.google.at|173.194.32.223|:81... failed: Connection timed out.
> Example with httpclient 4.3.3 (parts from our tracing):
> Exception<org.apache.http.conn.HttpHostConnectException> Message<Connect to **** failed: Die Wartezeit für die Verbindung ist abgelaufen>
> Cause<java.net.ConnectException: Die Wartezeit für die Verbindung ist abgelaufen>
> at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:138)
> at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:314)
> at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:357)
> at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:218)
> at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)
> at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
> at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
> at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
> at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
> at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
>
> We excpected to get a ConnectTimeoutException rather than a HttpHostConnectException
--
This message was sent by Atlassian JIRA
(v6.2#6252)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org