how to decide if SocketTimeoutException means there is no connection

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật