Skip to content

Commit 1dd37ad

Browse files
committed
Merge pull request #27 from recampbell/exceptions
Throw a NotFoundException when 404's are received
2 parents 7d5110d + f19fd19 commit 1dd37ad

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

src/main/java/com/kpelykh/docker/client/DockerClient.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public List<Image> getImages(String name, boolean allImages) throws DockerExcept
254254

255255
}
256256

257-
public ImageInspectResponse inspectImage(String imageId) throws DockerException {
257+
public ImageInspectResponse inspectImage(String imageId) throws DockerException, NotFoundException {
258258

259259
WebResource webResource = client.resource(restEndpointUrl + String.format("/images/%s/json", imageId));
260260

@@ -263,7 +263,7 @@ public ImageInspectResponse inspectImage(String imageId) throws DockerException
263263
return webResource.accept(MediaType.APPLICATION_JSON).get(ImageInspectResponse.class);
264264
} catch (UniformInterfaceException exception) {
265265
if (exception.getResponse().getStatus() == 404) {
266-
throw new DockerException(String.format("No such image %s", imageId));
266+
throw new NotFoundException(String.format("No such image %s", imageId));
267267
} else if (exception.getResponse().getStatus() == 500) {
268268
throw new DockerException("Server error", exception);
269269
} else {
@@ -318,7 +318,7 @@ public ContainerCreateResponse createContainer(ContainerConfig config) throws Do
318318
return createContainer(config, null);
319319
}
320320

321-
public ContainerCreateResponse createContainer(ContainerConfig config,String name) throws DockerException {
321+
public ContainerCreateResponse createContainer(ContainerConfig config,String name) throws DockerException, NotFoundException {
322322

323323
MultivaluedMap<String,String> params = new MultivaluedMapImpl();
324324
if(name != null){
@@ -333,7 +333,7 @@ public ContainerCreateResponse createContainer(ContainerConfig config,String nam
333333
.post(ContainerCreateResponse.class, config);
334334
} catch (UniformInterfaceException exception) {
335335
if (exception.getResponse().getStatus() == 404) {
336-
throw new DockerException(String.format("%s is an unrecognized image. Please pull the image first.", config.getImage()));
336+
throw new NotFoundException(String.format("%s is an unrecognized image. Please pull the image first.", config.getImage()));
337337
} else if (exception.getResponse().getStatus() == 406) {
338338
throw new DockerException("impossible to attach (container not running)");
339339
} else if (exception.getResponse().getStatus() == 500) {
@@ -349,7 +349,7 @@ public void startContainer(String containerId) throws DockerException {
349349
this.startContainer(containerId, null);
350350
}
351351

352-
public void startContainer(String containerId, HostConfig hostConfig) throws DockerException {
352+
public void startContainer(String containerId, HostConfig hostConfig) throws DockerException, NotFoundException {
353353

354354
WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/start", containerId));
355355

@@ -363,7 +363,7 @@ public void startContainer(String containerId, HostConfig hostConfig) throws Doc
363363
}
364364
} catch (UniformInterfaceException exception) {
365365
if (exception.getResponse().getStatus() == 404) {
366-
throw new DockerException(String.format("No such container %s", containerId));
366+
throw new NotFoundException(String.format("No such container %s", containerId));
367367
} else if (exception.getResponse().getStatus() == 204) {
368368
//no error
369369
LOGGER.trace("Successfully started container {}", containerId);
@@ -375,7 +375,7 @@ public void startContainer(String containerId, HostConfig hostConfig) throws Doc
375375
}
376376
}
377377

378-
public ContainerInspectResponse inspectContainer(String containerId) throws DockerException {
378+
public ContainerInspectResponse inspectContainer(String containerId) throws DockerException, NotFoundException {
379379

380380
WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/json", containerId));
381381

@@ -384,7 +384,7 @@ public ContainerInspectResponse inspectContainer(String containerId) throws Dock
384384
return webResource.accept(MediaType.APPLICATION_JSON).get(ContainerInspectResponse.class);
385385
} catch (UniformInterfaceException exception) {
386386
if (exception.getResponse().getStatus() == 404) {
387-
throw new DockerException(String.format("No such container %s", containerId));
387+
throw new NotFoundException(String.format("No such container %s", containerId));
388388
} else if (exception.getResponse().getStatus() == 500) {
389389
throw new DockerException("Server error", exception);
390390
} else {
@@ -414,6 +414,7 @@ public void removeContainer(String containerId, boolean removeVolumes) throws Do
414414
} else if (exception.getResponse().getStatus() == 400) {
415415
throw new DockerException("bad parameter");
416416
} else if (exception.getResponse().getStatus() == 404) {
417+
// should really throw a NotFoundException instead of silently ignoring the problem
417418
LOGGER.warn(String.format("%s is an unrecognized container.", containerId));
418419
} else if (exception.getResponse().getStatus() == 500) {
419420
throw new DockerException("Server error", exception);
@@ -432,7 +433,7 @@ public void removeContainers(List<String> containers, boolean removeVolumes) thr
432433
}
433434
}
434435

435-
public int waitContainer(String containerId) throws DockerException {
436+
public int waitContainer(String containerId) throws DockerException, NotFoundException {
436437
WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/wait", containerId));
437438

438439
try {
@@ -441,7 +442,7 @@ public int waitContainer(String containerId) throws DockerException {
441442
return jsonObject.getInt("StatusCode");
442443
} catch (UniformInterfaceException exception) {
443444
if (exception.getResponse().getStatus() == 404) {
444-
throw new DockerException(String.format("No such container %s", containerId));
445+
throw new NotFoundException(String.format("No such container %s", containerId));
445446
} else if (exception.getResponse().getStatus() == 500) {
446447
throw new DockerException("Server error", exception);
447448
} else {
@@ -461,7 +462,7 @@ public ClientResponse logContainerStream(String containerId) throws DockerExcept
461462
return logContainer(containerId, true);
462463
}
463464

464-
private ClientResponse logContainer(String containerId, boolean stream) throws DockerException {
465+
private ClientResponse logContainer(String containerId, boolean stream) throws DockerException, NotFoundException {
465466
MultivaluedMap<String,String> params = new MultivaluedMapImpl();
466467
params.add("logs", "1");
467468
params.add("stdout", "1");
@@ -480,7 +481,7 @@ private ClientResponse logContainer(String containerId, boolean stream) throws D
480481
if (exception.getResponse().getStatus() == 400) {
481482
throw new DockerException("bad parameter");
482483
} else if (exception.getResponse().getStatus() == 404) {
483-
throw new DockerException(String.format("No such container %s", containerId));
484+
throw new NotFoundException(String.format("No such container %s", containerId));
484485
} else if (exception.getResponse().getStatus() == 500) {
485486
throw new DockerException("Server error", exception);
486487
} else {
@@ -489,7 +490,7 @@ private ClientResponse logContainer(String containerId, boolean stream) throws D
489490
}
490491
}
491492

492-
public List<ChangeLog> containterDiff(String containerId) throws DockerException {
493+
public List<ChangeLog> containterDiff(String containerId) throws DockerException, NotFoundException {
493494

494495
WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/changes", containerId));
495496

@@ -498,7 +499,7 @@ public List<ChangeLog> containterDiff(String containerId) throws DockerException
498499
return webResource.accept(MediaType.APPLICATION_JSON).get(new GenericType<List<ChangeLog>>() {});
499500
} catch (UniformInterfaceException exception) {
500501
if (exception.getResponse().getStatus() == 404) {
501-
throw new DockerException(String.format("No such container %s", containerId));
502+
throw new NotFoundException(String.format("No such container %s", containerId));
502503
} else if (exception.getResponse().getStatus() == 500) {
503504
throw new DockerException("Server error", exception);
504505
} else {
@@ -557,15 +558,15 @@ public void kill(String containerId) throws DockerException {
557558
}
558559
}
559560

560-
public void restart(String containerId, int timeout) throws DockerException {
561+
public void restart(String containerId, int timeout) throws DockerException, NotFoundException {
561562
WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/restart", containerId));
562563

563564
try {
564565
LOGGER.trace("POST: {}", webResource);
565566
webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post();
566567
} catch (UniformInterfaceException exception) {
567568
if (exception.getResponse().getStatus() == 404) {
568-
throw new DockerException(String.format("No such container %s", containerId));
569+
throw new NotFoundException(String.format("No such container %s", containerId));
569570
} else if (exception.getResponse().getStatus() == 204) {
570571
//no error
571572
LOGGER.trace("Successfully restarted container {}", containerId);
@@ -577,7 +578,7 @@ public void restart(String containerId, int timeout) throws DockerException {
577578
}
578579
}
579580

580-
public String commit(CommitConfig commitConfig) throws DockerException {
581+
public String commit(CommitConfig commitConfig) throws DockerException, NotFoundException {
581582
Preconditions.checkNotNull(commitConfig.getContainer(), "Container ID was not specified");
582583

583584
MultivaluedMap<String,String> params = new MultivaluedMapImpl();
@@ -596,7 +597,7 @@ public String commit(CommitConfig commitConfig) throws DockerException {
596597
return jsonObject.getString("Id");
597598
} catch (UniformInterfaceException exception) {
598599
if (exception.getResponse().getStatus() == 404) {
599-
throw new DockerException(String.format("No such container %s", commitConfig.getContainer()));
600+
throw new NotFoundException(String.format("No such container %s", commitConfig.getContainer()));
600601
} else if (exception.getResponse().getStatus() == 500) {
601602
throw new DockerException("Server error", exception);
602603
} else {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.kpelykh.docker.client;
2+
3+
/**
4+
* Indicates that the given entity does not exist.
5+
*
6+
* @author Ryan Campbell ryan.campbell@gmail.com
7+
*/
8+
public class NotFoundException extends DockerException {
9+
public NotFoundException() {
10+
11+
}
12+
13+
public NotFoundException(String message) {
14+
super(message);
15+
}
16+
17+
public NotFoundException(String message, Throwable cause) {
18+
super(message, cause);
19+
}
20+
}

0 commit comments

Comments
 (0)