From 4225ec7060839f53be1577d36f025b5b72e868ae Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 15 Jul 2024 16:39:40 +0200 Subject: [PATCH] feat: Improve error handling in loadComposeFile method --- app/Models/Application.php | 66 ++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/app/Models/Application.php b/app/Models/Application.php index b8bffa66a..81fdb5a1e 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -1081,45 +1081,55 @@ public function loadComposeFile($isInit = false) 'git read-tree -mu HEAD', "cat .$workdir$composeFile", ]); - $composeFileContent = instant_remote_process($commands, $this->destination->server, false); - if (! $composeFileContent) { + try { + $composeFileContent = instant_remote_process($commands, $this->destination->server); + } catch (\Exception $e) { + if (str($e->getMessage())->contains('No such file')) { + throw new \RuntimeException("Docker Compose file not found at: $workdir$composeFile

Check if you used the right extension (.yaml or .yml) in the compose file name."); + } + if (str($e->getMessage())->contains('fatal: repository') && str($e->getMessage())->contains('does not exist')) { + if ($this->deploymentType() === 'deploy_key') { + throw new \RuntimeException('Your deploy key does not have access to the repository. Please check your deploy key and try again.'); + } + throw new \RuntimeException('Repository does not exist. Please check your repository URL and try again.'); + } + throw new \RuntimeException($e->getMessage()); + } finally { $this->docker_compose_location = $initialDockerComposeLocation; $this->save(); $commands = collect([ "rm -rf /tmp/{$uuid}", ]); instant_remote_process($commands, $this->destination->server, false); - throw new \RuntimeException("Docker Compose file not found at: $workdir$composeFile

Check if you used the right extension (.yaml or .yml) in the compose file name."); - } else { + } + if ($composeFileContent) { $this->docker_compose_raw = $composeFileContent; $this->save(); - } - - $commands = collect([ - "rm -rf /tmp/{$uuid}", - ]); - instant_remote_process($commands, $this->destination->server, false); - $parsedServices = $this->parseCompose(); - if ($this->docker_compose_domains) { - $json = collect(json_decode($this->docker_compose_domains)); - $names = collect(data_get($parsedServices, 'services'))->keys()->toArray(); - $jsonNames = $json->keys()->toArray(); - $diff = array_diff($jsonNames, $names); - $json = $json->filter(function ($value, $key) use ($diff) { - return ! in_array($key, $diff); - }); - if ($json) { - $this->docker_compose_domains = json_encode($json); - } else { - $this->docker_compose_domains = null; + $parsedServices = $this->parseCompose(); + if ($this->docker_compose_domains) { + $json = collect(json_decode($this->docker_compose_domains)); + $names = collect(data_get($parsedServices, 'services'))->keys()->toArray(); + $jsonNames = $json->keys()->toArray(); + $diff = array_diff($jsonNames, $names); + $json = $json->filter(function ($value, $key) use ($diff) { + return ! in_array($key, $diff); + }); + if ($json) { + $this->docker_compose_domains = json_encode($json); + } else { + $this->docker_compose_domains = null; + } + $this->save(); } - $this->save(); + + return [ + 'parsedServices' => $parsedServices, + 'initialDockerComposeLocation' => $this->docker_compose_location, + ]; + } else { + throw new \RuntimeException("Docker Compose file not found at: $workdir$composeFile

Check if you used the right extension (.yaml or .yml) in the compose file name."); } - return [ - 'parsedServices' => $parsedServices, - 'initialDockerComposeLocation' => $this->docker_compose_location, - ]; } public function parseContainerLabels(?ApplicationPreview $preview = null)