we have a retrofit java client to a server running in gui app, we want to notify the user if there is no connection to the server, there are some exceptions that are obvious like ConnectException or UnknownHostException etc.
but on timeouts we don’t want to do nothing , the next api call will probably succeed.
my main question is about SocketTimeoutException, when does it mean that the server is not reachable?
sometimes the message is ‘Connect timed out’ and sometimes its ‘timeout’
so how to differentiate between a timeout and when the server is not reachable?
for now i’v written this code:
public static boolean isConnectionUnavailableException(@NotNull Throwable exception) {
//these are considered just timeout
if ((SocketTimeoutException.class.equals(exception.getClass()) && !isConnectTimeout(exception)) ||
HttpTimeoutException.class.equals(exception.getClass()) ||
isIOExceptionTimeout(exception)) {
return false;
}
return isConnectTimeout(exception) ||
isConnectionIssueErrorCode(exception) ||
exception instanceof SocketException ||
exception instanceof UnknownHostException ||
exception instanceof HttpConnectTimeoutException ||
exception instanceof InterruptedIOException;
}
private static boolean isConnectionIssueErrorCode(@NotNull Throwable exception){
var analyticsProviderException = findCause(AnalyticsProviderException.class,exception);
if (analyticsProviderException != null){
var connectionIssueCodes = List.of(503,504,502);
return connectionIssueCodes.contains(analyticsProviderException.getResponseCode());
}
return false;
}
private static boolean isConnectTimeout(@NotNull Throwable exception){
return SocketTimeoutException.class.equals(exception.getClass()) && "Connect timed out".equalsIgnoreCase(exception.getMessage());
}
private static boolean isIOExceptionTimeout(@NotNull Throwable exception) {
return IOException.class.equals(exception.getClass()) &&
exception.getMessage() != null &&
exception.getMessage().toLowerCase().contains("timeout");
}
You can always look into what the message as well as the number of bytes transferred. If the number of bytes transferred is 0 and the message does not say otherwise, then it’s safe to conclude that you did not get a response in time. If you received some bytes, then the server is working but the response took too long to arrive.
It’s always possible that the server received your request but it had a long algorithm to process. So it’s always a good idea to have a lightweight end point on your server that you can ping (or simply ping the address if that’s feasible) when the exception’s cause is not clear, retrying that.
0