Search in sources :

Example 36 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method propagate.

private RuntimeException propagate(final String method, final WebTarget resource, final Exception ex) throws DockerException, InterruptedException {
    Throwable cause = ex.getCause();
    // So we unpack it here.
    if (ex instanceof MultiException) {
        cause = cause.getCause();
    }
    Response response = null;
    if (cause instanceof ResponseProcessingException) {
        response = ((ResponseProcessingException) cause).getResponse();
    } else if (cause instanceof WebApplicationException) {
        response = ((WebApplicationException) cause).getResponse();
    } else if ((cause instanceof ProcessingException) && (cause.getCause() != null)) {
        // For a ProcessingException, The exception message or nested Throwable cause SHOULD contain
        // additional information about the reason of the processing failure.
        cause = cause.getCause();
    }
    if (response != null) {
        throw new DockerRequestException(method, resource.getUri(), response.getStatus(), message(response), cause);
    } else if ((cause instanceof SocketTimeoutException) || (cause instanceof ConnectTimeoutException)) {
        throw new DockerTimeoutException(method, resource.getUri(), ex);
    } else if ((cause instanceof InterruptedIOException) || (cause instanceof InterruptedException)) {
        throw new InterruptedException("Interrupted: " + method + " " + resource);
    } else {
        throw new DockerException(ex);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) InterruptedIOException(java.io.InterruptedIOException) WebApplicationException(javax.ws.rs.WebApplicationException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ConfigCreateResponse(com.spotify.docker.client.messages.swarm.ConfigCreateResponse) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SecretCreateResponse(com.spotify.docker.client.messages.swarm.SecretCreateResponse) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) SocketTimeoutException(java.net.SocketTimeoutException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) MultiException(org.glassfish.hk2.api.MultiException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 37 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method push.

@Override
public void push(final String image, final ProgressHandler handler, final RegistryAuth registryAuth) throws DockerException, InterruptedException {
    final ImageRef imageRef = new ImageRef(image);
    WebTarget resource = resource().path("images").path(imageRef.getImage()).path("push");
    if (imageRef.getTag() != null) {
        resource = resource.queryParam("tag", imageRef.getTag());
    }
    try (ProgressStream push = request(POST, ProgressStream.class, resource, resource.request(APPLICATION_JSON_TYPE).header("X-Registry-Auth", authHeader(registryAuth)))) {
        push.tail(handler, POST, resource.getUri());
    } catch (IOException e) {
        throw new DockerException(e);
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ImageNotFoundException(image, e);
            default:
                throw e;
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Example 38 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method execStart.

@Override
public LogStream execStart(final String execId, final ExecStartParameter... params) throws DockerException, InterruptedException {
    final WebTarget resource = noTimeoutResource().path("exec").path(execId).path("start");
    final StringWriter writer = new StringWriter();
    try {
        final JsonGenerator generator = objectMapper().getFactory().createGenerator(writer);
        generator.writeStartObject();
        for (final ExecStartParameter param : params) {
            generator.writeBooleanField(param.getName(), true);
        }
        generator.writeEndObject();
        generator.close();
    } catch (IOException e) {
        throw new DockerException(e);
    }
    try {
        return request(POST, LogStream.class, resource, resource.request("application/vnd.docker.raw-stream"), Entity.json(writer.toString()));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ExecNotFoundException(execId, e);
            case 409:
                throw new ExecStartConflictException(execId, e);
            default:
                throw e;
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) StringWriter(java.io.StringWriter) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) WebTarget(javax.ws.rs.client.WebTarget) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecNotFoundException(com.spotify.docker.client.exceptions.ExecNotFoundException) ExecStartConflictException(com.spotify.docker.client.exceptions.ExecStartConflictException)

Example 39 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method commitContainer.

@Override
public ContainerCreation commitContainer(final String containerId, final String repo, final String tag, final ContainerConfig config, final String comment, final String author) throws DockerException, InterruptedException {
    checkNotNull(containerId, "containerId");
    checkNotNull(repo, "repo");
    checkNotNull(config, "containerConfig");
    WebTarget resource = resource().path("commit").queryParam("container", containerId).queryParam("repo", repo);
    if (!isNullOrEmpty(author)) {
        resource = resource.queryParam("author", author);
    }
    if (!isNullOrEmpty(comment)) {
        resource = resource.queryParam("comment", comment);
    }
    if (!isNullOrEmpty(tag)) {
        resource = resource.queryParam("tag", tag);
    }
    log.debug("Committing container id: {} to repository: {} with ContainerConfig: {}", containerId, repo, config);
    try {
        return request(POST, ContainerCreation.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(config));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ContainerNotFoundException(containerId, e);
            default:
                throw e;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException)

Example 40 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method removeService.

@Override
public void removeService(final String serviceId) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.24");
    try {
        final WebTarget resource = resource().path("services").path(serviceId);
        request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new ServiceNotFoundException(serviceId);
            default:
                throw e;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) ServiceNotFoundException(com.spotify.docker.client.exceptions.ServiceNotFoundException) WebTarget(javax.ws.rs.client.WebTarget)

Aggregations

DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)45 WebTarget (javax.ws.rs.client.WebTarget)37 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)18 DockerException (com.spotify.docker.client.exceptions.DockerException)14 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)12 NonSwarmNodeException (com.spotify.docker.client.exceptions.NonSwarmNodeException)11 NetworkNotFoundException (com.spotify.docker.client.exceptions.NetworkNotFoundException)9 NodeNotFoundException (com.spotify.docker.client.exceptions.NodeNotFoundException)9 ExecNotFoundException (com.spotify.docker.client.exceptions.ExecNotFoundException)8 NotFoundException (com.spotify.docker.client.exceptions.NotFoundException)8 ServiceNotFoundException (com.spotify.docker.client.exceptions.ServiceNotFoundException)8 VolumeNotFoundException (com.spotify.docker.client.exceptions.VolumeNotFoundException)8 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)7 TaskNotFoundException (com.spotify.docker.client.exceptions.TaskNotFoundException)6 Test (org.junit.Test)6 BadParamException (com.spotify.docker.client.exceptions.BadParamException)5 ConflictException (com.spotify.docker.client.exceptions.ConflictException)5 IOException (java.io.IOException)5 ExecCreateConflictException (com.spotify.docker.client.exceptions.ExecCreateConflictException)4 ExecStartConflictException (com.spotify.docker.client.exceptions.ExecStartConflictException)4