Search in sources :

Example 1 with HeliosException

use of com.spotify.helios.common.HeliosException in project helios by spotify.

the class AuthenticatingHttpConnector method connect.

@Override
public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException {
    final Endpoint endpoint = endpointIterator.next();
    // convert the URI whose hostname portion is a domain name into a URI where the host is an IP
    // as we expect there to be several different IP addresses besides a common domain name
    final URI ipUri;
    try {
        ipUri = toIpUri(endpoint, uri);
    } catch (URISyntaxException e) {
        throw new HeliosException(e);
    }
    try {
        log.debug("connecting to {}", ipUri);
        if (clientCertificatePath.isPresent()) {
            // prioritize using the certificate file if set
            return connectWithCertificateFile(ipUri, method, entity, headers);
        } else if (agentProxy.isPresent() && !identities.isEmpty()) {
            // ssh-agent based authentication
            return connectWithIdentities(identities, ipUri, method, entity, headers);
        } else {
            // no authentication
            return doConnect(ipUri, method, entity, headers);
        }
    } catch (ConnectException | SocketTimeoutException | UnknownHostException e) {
        // UnknownHostException happens if we can't resolve hostname into IP address.
        // UnknownHostException's getMessage method returns just the hostname which is a
        // useless message, so log the exception class name to provide more info.
        log.debug(e.toString());
        throw new HeliosException("Unable to connect to master: " + ipUri, e);
    } catch (IOException e) {
        throw new HeliosException("Unexpected error connecting to " + ipUri, e);
    }
}
Also used : HeliosException(com.spotify.helios.common.HeliosException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ConnectException(java.net.ConnectException)

Example 2 with HeliosException

use of com.spotify.helios.common.HeliosException in project helios by spotify.

the class DefaultHttpConnector method connect.

@Override
public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException {
    final Endpoint endpoint = endpointIterator.next();
    final String endpointHost = endpoint.getUri().getHost();
    try {
        final HttpURLConnection connection = connect0(uri, method, entity, headers, endpointHost);
        if (connection.getResponseCode() == HTTP_BAD_GATEWAY) {
            throw new HeliosException(String.format("Request to %s returned %s, master is down", uri, connection.getResponseCode()));
        }
        return connection;
    } catch (ConnectException | SocketTimeoutException | UnknownHostException e) {
        // UnknownHostException happens if we can't resolve hostname into IP address.
        // UnknownHostException's getMessage method returns just the hostname which is a
        // useless message, so log the exception class name to provide more info.
        log.debug(e.toString());
        throw new HeliosException("Unable to connect to master: " + uri, e);
    } catch (IOException e) {
        throw new HeliosException("Unexpected error connecting to " + uri, e);
    }
}
Also used : HeliosException(com.spotify.helios.common.HeliosException) HttpURLConnection(java.net.HttpURLConnection) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Example 3 with HeliosException

use of com.spotify.helios.common.HeliosException in project helios by spotify.

the class MasterRespondsWithNoZkTest method test.

@Test
public void test() throws Exception {
    startDefaultMasterDontWaitForZk(new MockCuratorClientFactory(), "--zk-connection-timeout", "1");
    final HeliosClient client = defaultClient();
    try {
        final String result = client.listMasters().get().get(0);
        fail("Exception should have been thrown, as ZK doesnt exist - got " + result);
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof HeliosException);
    }
}
Also used : HeliosException(com.spotify.helios.common.HeliosException) Matchers.anyString(org.mockito.Matchers.anyString) HeliosClient(com.spotify.helios.client.HeliosClient) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

HeliosException (com.spotify.helios.common.HeliosException)3 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 UnknownHostException (java.net.UnknownHostException)2 HeliosClient (com.spotify.helios.client.HeliosClient)1 HttpURLConnection (java.net.HttpURLConnection)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1