diff --git a/app/Repositories/Daemon/DaemonFileRepository.php b/app/Repositories/Daemon/DaemonFileRepository.php index 650d8447a7..15e52fcdb8 100644 --- a/app/Repositories/Daemon/DaemonFileRepository.php +++ b/app/Repositories/Daemon/DaemonFileRepository.php @@ -7,6 +7,7 @@ use App\Exceptions\Repository\FileNotEditableException; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Http\Client\ConnectionException; +use Illuminate\Http\Client\RequestException; use Illuminate\Http\Client\Response; class DaemonFileRepository extends DaemonRepository @@ -22,21 +23,27 @@ class DaemonFileRepository extends DaemonRepository */ public function getContent(string $path, ?int $notLargerThan = null): string { - $response = $this->getHttpClient()->get("/api/servers/{$this->server->uuid}/files/contents", - ['file' => $path] - ); + try { + $response = $this->getHttpClient()->get("/api/servers/{$this->server->uuid}/files/contents", + ['file' => $path] + ); + } catch (RequestException $exception) { + $status = $exception->response->status(); - $length = $response->header('Content-Length'); - if ($notLargerThan && $length > $notLargerThan) { - throw new FileSizeTooLargeException(); - } + if ($status === 400) { + throw new FileNotEditableException(); + } + + if ($status === 404) { + throw new FileNotFoundException(); + } - if ($response->status() === 400) { - throw new FileNotEditableException(); + throw $exception; } - if ($response->status() === 404) { - throw new FileNotFoundException(); + $length = $response->header('Content-Length'); + if ($notLargerThan && $length > $notLargerThan) { + throw new FileSizeTooLargeException(); } return $response; @@ -51,16 +58,18 @@ public function getContent(string $path, ?int $notLargerThan = null): string */ public function putContent(string $path, string $content): Response { - $response = $this->getHttpClient() - ->withQueryParameters(['file' => $path]) - ->withBody($content) - ->post("/api/servers/{$this->server->uuid}/files/write"); + try { + return $this->getHttpClient() + ->withQueryParameters(['file' => $path]) + ->withBody($content) + ->post("/api/servers/{$this->server->uuid}/files/write"); + } catch (RequestException $exception) { + if ($exception->response->status() === 400) { + throw new FileExistsException(); + } - if ($response->status() === 400) { - throw new FileExistsException(); + throw $exception; } - - return $response; } /** @@ -85,18 +94,20 @@ public function getDirectory(string $path): array */ public function createDirectory(string $name, string $path): Response { - $response = $this->getHttpClient()->post("/api/servers/{$this->server->uuid}/files/create-directory", - [ - 'name' => $name, - 'path' => $path, - ] - ); + try { + return $this->getHttpClient()->post("/api/servers/{$this->server->uuid}/files/create-directory", + [ + 'name' => $name, + 'path' => $path, + ] + ); + } catch (RequestException $exception) { + if ($exception->response->status() === 400) { + throw new FileExistsException(); + } - if ($response->status() === 400) { - throw new FileExistsException(); + throw $exception; } - - return $response; } /** diff --git a/app/Repositories/Daemon/DaemonRepository.php b/app/Repositories/Daemon/DaemonRepository.php index f8b85e0d1d..18bfdbafe0 100644 --- a/app/Repositories/Daemon/DaemonRepository.php +++ b/app/Repositories/Daemon/DaemonRepository.php @@ -57,9 +57,6 @@ protected function enforceValidNodeToken(Response|bool $condition): bool if (is_bool($condition)) { return $condition; } - if ($condition->clientError()) { - return false; - } $header = $condition->header('User-Agent'); if (