Skip to content

Commit

Permalink
Improve error handling when client version not supported, or other AP…
Browse files Browse the repository at this point in the history
…I error

Signed-off-by: Grant Millar <[email protected]>
  • Loading branch information
Rid committed Oct 18, 2023
1 parent 4a2b826 commit 37c3f53
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Docker\API\Client;
use Docker\API\Model\AuthConfig;
use Docker\Endpoint\ImagePush;
use Docker\API\Runtime\Client\Client as RuntimeClient;
use Docker\API\Exception\BadRequestException;

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

return parent::create($httpClient, $additionalPlugins, $additionalNormalizers);
$client = parent::create($httpClient, $additionalPlugins, $additionalNormalizers);
$testClient = $client->systemInfo(RuntimeClient::FETCH_RESPONSE)->getBody()->getContents();
$jsonObj = json_decode($testClient);

if ($jsonObj !== null) {
if (isset($jsonObj->message)) {
// Check if the client is too new
if (strpos($jsonObj->message, 'client version') !== false && strpos($jsonObj->message, 'is too new') !== false) {
throw new BadRequestException("The client version is not supported by your version of Docker. Message: {$jsonObj->message}");
} else {
throw new BadRequestException($jsonObj->message);
}
}
} else {
throw new BadRequestException("Failed to decode JSON.");
}
return $client;
}
}

0 comments on commit 37c3f53

Please sign in to comment.