Skip to content

Commit 37c3f53

Browse files
committed
Improve error handling when client version not supported, or other API error
Signed-off-by: Grant Millar <rid@cylo.io>
1 parent 4a2b826 commit 37c3f53

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/Docker.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Docker\API\Client;
88
use Docker\API\Model\AuthConfig;
99
use Docker\Endpoint\ImagePush;
10+
use Docker\API\Runtime\Client\Client as RuntimeClient;
11+
use Docker\API\Exception\BadRequestException;
1012

1113
/**
1214
* Docker\Docker.
@@ -28,6 +30,22 @@ public static function create($httpClient = null, array $additionalPlugins = [],
2830
$httpClient = DockerClientFactory::createFromEnv();
2931
}
3032

31-
return parent::create($httpClient, $additionalPlugins, $additionalNormalizers);
33+
$client = parent::create($httpClient, $additionalPlugins, $additionalNormalizers);
34+
$testClient = $client->systemInfo(RuntimeClient::FETCH_RESPONSE)->getBody()->getContents();
35+
$jsonObj = json_decode($testClient);
36+
37+
if ($jsonObj !== null) {
38+
if (isset($jsonObj->message)) {
39+
// Check if the client is too new
40+
if (strpos($jsonObj->message, 'client version') !== false && strpos($jsonObj->message, 'is too new') !== false) {
41+
throw new BadRequestException("The client version is not supported by your version of Docker. Message: {$jsonObj->message}");
42+
} else {
43+
throw new BadRequestException($jsonObj->message);
44+
}
45+
}
46+
} else {
47+
throw new BadRequestException("Failed to decode JSON.");
48+
}
49+
return $client;
3250
}
3351
}

0 commit comments

Comments
 (0)