aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/configserver/ConnectionException.java
blob: 86c52efe28205a7bf50f081ddcdb90ae50e5ad11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.configserver;

import com.yahoo.vespa.hosted.node.admin.nodeadmin.ConvergenceException;
import org.apache.http.NoHttpResponseException;

import java.io.EOFException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

/**
 * @author freva
 */
@SuppressWarnings("serial")
public class ConnectionException extends ConvergenceException {

    private ConnectionException(String message, Throwable cause) {
        super(message, cause, true);
    }

    /**
     * Returns {@link ConnectionException} if the given Throwable is of a known and well understood error or
     * a RuntimeException with the given exception as cause otherwise.
     */
    public static RuntimeException handleException(String prefix, Throwable t) {
        if (isKnownConnectionException(t))
            return new ConnectionException(prefix + t.getMessage(), t);

        return new RuntimeException(prefix, t);
    }

    static boolean isKnownConnectionException(Throwable t) {
        for (; t != null; t = t.getCause()) {
            if (t instanceof SocketException ||
                    t instanceof SocketTimeoutException ||
                    t instanceof NoHttpResponseException ||
                    t instanceof EOFException)
                return true;
        }

        return false;
    }
}