From 37c3f53e9f59e101477f4e1e5ae4443c290f418b Mon Sep 17 00:00:00 2001 From: Grant Millar Date: Wed, 18 Oct 2023 16:35:42 +0200 Subject: [PATCH] Improve error handling when client version not supported, or other API error Signed-off-by: Grant Millar --- src/Docker.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Docker.php b/src/Docker.php index ba4ce786..ddac2694 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -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. @@ -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; } }