rework exception handling in dockeractionmanager

Signed-off-by: szaimen <szaimen@e.mail.de>
This commit is contained in:
szaimen 2022-01-20 13:30:39 +01:00
parent 4603becc1c
commit 49cf819f2f

View file

@ -11,8 +11,7 @@ use AIO\Container\State\VersionDifferentState;
use AIO\Container\State\StoppedState; use AIO\Container\State\StoppedState;
use AIO\Container\State\VersionEqualState; use AIO\Container\State\VersionEqualState;
use AIO\Data\ConfigurationManager; use AIO\Data\ConfigurationManager;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use AIO\ContainerDefinitionFetcher; use AIO\ContainerDefinitionFetcher;
use http\Env\Response; use http\Env\Response;
@ -55,12 +54,11 @@ class DockerActionManager
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier()))); $url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier())));
try { try {
$response = $this->guzzleClient->get($url); $response = $this->guzzleClient->get($url);
} catch (ClientException $ex) { } catch (RequestException $e) {
if($ex->getCode() === 404) { if ($e->getCode() === 404) {
return new ImageDoesNotExistState(); return new ImageDoesNotExistState();
} }
throw $e;
throw $ex;
} }
$responseBody = json_decode((string)$response->getBody(), true); $responseBody = json_decode((string)$response->getBody(), true);
@ -115,7 +113,7 @@ class DockerActionManager
$url = $this->BuildApiUrl(sprintf('containers/%s', urlencode($container->GetIdentifier()))); $url = $this->BuildApiUrl(sprintf('containers/%s', urlencode($container->GetIdentifier())));
try { try {
$this->guzzleClient->delete($url); $this->guzzleClient->delete($url);
} catch (\GuzzleHttp\Exception\RequestException $e) { } catch (RequestException $e) {
if ($e->getCode() !== 404) { if ($e->getCode() !== 404) {
throw $e; throw $e;
} }
@ -262,8 +260,8 @@ class DockerActionManager
$url = $this->BuildApiUrl(sprintf('images/create?fromImage=%s', urlencode($this->BuildImageName($container)))); $url = $this->BuildApiUrl(sprintf('images/create?fromImage=%s', urlencode($this->BuildImageName($container))));
try { try {
$this->guzzleClient->post($url); $this->guzzleClient->post($url);
} catch (ClientException $ex) { } catch (RequestException $e) {
throw $ex; throw $e;
} }
} }
@ -329,7 +327,7 @@ class DockerActionManager
$tag = $tagArray[1]; $tag = $tagArray[1];
apcu_add($cacheKey, $tag); apcu_add($cacheKey, $tag);
return $tag; return $tag;
} catch (\Exception $ex) { } catch (\Exception $e) {
} }
return 'latest'; return 'latest';
@ -415,7 +413,11 @@ class DockerActionManager
], ],
] ]
); );
} catch (\GuzzleHttp\Exception\RequestException $e) {} } catch (RequestException $e) {
if ($e->getCode() !== 404) {
throw $e;
}
}
} }
private function ConnectContainerIdToNetwork(string $id) private function ConnectContainerIdToNetwork(string $id)
@ -433,10 +435,8 @@ class DockerActionManager
] ]
] ]
); );
} catch (ClientException $e) { } catch (RequestException $e) {
if($e->getCode() !== 409) { throw $e;
throw $e;
}
} }
$url = $this->BuildApiUrl( $url = $this->BuildApiUrl(
@ -452,10 +452,8 @@ class DockerActionManager
] ]
] ]
); );
} catch (ClientException $e) { } catch (RequestException $e) {
if($e->getCode() !== 403) { throw $e;
throw $e;
}
} }
} }
@ -473,7 +471,7 @@ class DockerActionManager
$url = $this->BuildApiUrl(sprintf('containers/%s/stop?t=%s', urlencode($container->GetIdentifier()), $container->GetMaxShutdownTime())); $url = $this->BuildApiUrl(sprintf('containers/%s/stop?t=%s', urlencode($container->GetIdentifier()), $container->GetMaxShutdownTime()));
try { try {
$this->guzzleClient->post($url); $this->guzzleClient->post($url);
} catch (\GuzzleHttp\Exception\RequestException $e) { } catch (RequestException $e) {
if ($e->getCode() !== 404 && $e->getCode() !== 304) { if ($e->getCode() !== 404 && $e->getCode() !== 304) {
throw $e; throw $e;
} }
@ -486,11 +484,11 @@ class DockerActionManager
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($containerName))); $url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($containerName)));
try { try {
$response = $this->guzzleClient->get($url); $response = $this->guzzleClient->get($url);
} catch (ClientException $ex) { } catch (RequestException $e) {
if ($ex->getCode() === 404) { if ($e->getCode() === 404) {
return -1; return -1;
} }
throw $ex; throw $e;
} }
$responseBody = json_decode((string)$response->getBody(), true); $responseBody = json_decode((string)$response->getBody(), true);