Skip to content

Commit

Permalink
Merge pull request #156 from nextcloud/enh/28/rework-exception-handling
Browse files Browse the repository at this point in the history
rework exception handling in dockeractionmanager
  • Loading branch information
szaimen committed Jan 20, 2022
2 parents 4603bec + 49cf819 commit bfc8526
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions php/src/Docker/DockerActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use AIO\Container\State\StoppedState;
use AIO\Container\State\VersionEqualState;
use AIO\Data\ConfigurationManager;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\RequestException;
use AIO\ContainerDefinitionFetcher;
use http\Env\Response;

Expand Down Expand Up @@ -55,12 +54,11 @@ public function GetContainerRunningState(Container $container) : IContainerState
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier())));
try {
$response = $this->guzzleClient->get($url);
} catch (ClientException $ex) {
if($ex->getCode() === 404) {
} catch (RequestException $e) {
if ($e->getCode() === 404) {
return new ImageDoesNotExistState();
}

throw $ex;
throw $e;
}

$responseBody = json_decode((string)$response->getBody(), true);
Expand Down Expand Up @@ -115,7 +113,7 @@ public function DeleteContainer(Container $container) {
$url = $this->BuildApiUrl(sprintf('containers/%s', urlencode($container->GetIdentifier())));
try {
$this->guzzleClient->delete($url);
} catch (\GuzzleHttp\Exception\RequestException $e) {
} catch (RequestException $e) {
if ($e->getCode() !== 404) {
throw $e;
}
Expand Down Expand Up @@ -262,8 +260,8 @@ public function PullContainer(Container $container)
$url = $this->BuildApiUrl(sprintf('images/create?fromImage=%s', urlencode($this->BuildImageName($container))));
try {
$this->guzzleClient->post($url);
} catch (ClientException $ex) {
throw $ex;
} catch (RequestException $e) {
throw $e;
}
}

Expand Down Expand Up @@ -329,7 +327,7 @@ public function GetCurrentChannel() : string {
$tag = $tagArray[1];
apcu_add($cacheKey, $tag);
return $tag;
} catch (\Exception $ex) {
} catch (\Exception $e) {
}

return 'latest';
Expand Down Expand Up @@ -415,7 +413,11 @@ public function DisconnectContainerFromNetwork(Container $container)
],
]
);
} catch (\GuzzleHttp\Exception\RequestException $e) {}
} catch (RequestException $e) {
if ($e->getCode() !== 404) {
throw $e;
}
}
}

private function ConnectContainerIdToNetwork(string $id)
Expand All @@ -433,10 +435,8 @@ private function ConnectContainerIdToNetwork(string $id)
]
]
);
} catch (ClientException $e) {
if($e->getCode() !== 409) {
throw $e;
}
} catch (RequestException $e) {
throw $e;
}

$url = $this->BuildApiUrl(
Expand All @@ -452,10 +452,8 @@ private function ConnectContainerIdToNetwork(string $id)
]
]
);
} catch (ClientException $e) {
if($e->getCode() !== 403) {
throw $e;
}
} catch (RequestException $e) {
throw $e;
}
}

Expand All @@ -473,7 +471,7 @@ public function StopContainer(Container $container) {
$url = $this->BuildApiUrl(sprintf('containers/%s/stop?t=%s', urlencode($container->GetIdentifier()), $container->GetMaxShutdownTime()));
try {
$this->guzzleClient->post($url);
} catch (\GuzzleHttp\Exception\RequestException $e) {
} catch (RequestException $e) {
if ($e->getCode() !== 404 && $e->getCode() !== 304) {
throw $e;
}
Expand All @@ -486,11 +484,11 @@ public function GetBackupcontainerExitCode() : int
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($containerName)));
try {
$response = $this->guzzleClient->get($url);
} catch (ClientException $ex) {
if ($ex->getCode() === 404) {
} catch (RequestException $e) {
if ($e->getCode() === 404) {
return -1;
}
throw $ex;
throw $e;
}

$responseBody = json_decode((string)$response->getBody(), true);
Expand Down

0 comments on commit bfc8526

Please sign in to comment.