Skip to content

Commit 62cd03d

Browse files
committed
Fix command sending error handling and bad assertion order
1 parent ee0da20 commit 62cd03d

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

app/Http/Controllers/Api/Client/Servers/CommandController.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use Illuminate\Http\Response;
66
use Pterodactyl\Models\Server;
77
use Psr\Http\Message\ResponseInterface;
8-
use GuzzleHttp\Exception\ClientException;
98
use GuzzleHttp\Exception\RequestException;
9+
use GuzzleHttp\Exception\BadResponseException;
10+
use Symfony\Component\HttpKernel\Exception\HttpException;
1011
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
1112
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1213
use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest;
1314
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
14-
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
1515

1616
class CommandController extends ClientApiController
1717
{
@@ -47,9 +47,14 @@ public function index(SendCommandRequest $request): Response
4747
try {
4848
$this->repository->setServer($server)->send($request->input('command'));
4949
} catch (RequestException $exception) {
50-
if ($exception instanceof ClientException) {
51-
if ($exception->getResponse() instanceof ResponseInterface && $exception->getResponse()->getStatusCode() === 412) {
52-
throw new PreconditionFailedHttpException('Server is not online.');
50+
if ($exception instanceof BadResponseException) {
51+
if (
52+
$exception->getResponse() instanceof ResponseInterface
53+
&& $exception->getResponse()->getStatusCode() === Response::HTTP_BAD_GATEWAY
54+
) {
55+
throw new HttpException(
56+
Response::HTTP_BAD_GATEWAY, 'Server must be online in order to send commands.', $exception
57+
);
5358
}
5459
}
5560

app/Repositories/Wings/DaemonCommandRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class DaemonCommandRepository extends DaemonRepository
1616
*/
1717
public function send($command): ResponseInterface
1818
{
19-
Assert::isInstanceOf(Server::class, $this->server);
19+
Assert::isInstanceOf($this->server, Server::class);
2020

2121
return $this->getHttpClient()->post(
2222
sprintf('/api/servers/%s/commands', $this->server->uuid),

app/Repositories/Wings/DaemonFileRepository.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getFileStat(string $path): stdClass
3737
*/
3838
public function getContent(string $path, int $notLargerThan = null): string
3939
{
40-
Assert::isInstanceOf(Server::class, $this->server);
40+
Assert::isInstanceOf($this->server, Server::class);
4141

4242
$response = $this->getHttpClient()->get(
4343
sprintf('/api/servers/%s/files/contents', $this->server->uuid),
@@ -69,7 +69,7 @@ public function getContent(string $path, int $notLargerThan = null): string
6969
*/
7070
public function putContent(string $path, string $content): ResponseInterface
7171
{
72-
Assert::isInstanceOf(Server::class, $this->server);
72+
Assert::isInstanceOf($this->server, Server::class);
7373

7474
return $this->getHttpClient()->post(
7575
sprintf('/api/servers/%s/files/write', $this->server->uuid),
@@ -90,7 +90,7 @@ public function putContent(string $path, string $content): ResponseInterface
9090
*/
9191
public function getDirectory(string $path): array
9292
{
93-
Assert::isInstanceOf(Server::class, $this->server);
93+
Assert::isInstanceOf($this->server, Server::class);
9494

9595
$response = $this->getHttpClient()->get(
9696
sprintf('/api/servers/%s/files/list-directory', $this->server->uuid),
@@ -111,7 +111,7 @@ public function getDirectory(string $path): array
111111
*/
112112
public function createDirectory(string $name, string $path): ResponseInterface
113113
{
114-
Assert::isInstanceOf(Server::class, $this->server);
114+
Assert::isInstanceOf($this->server, Server::class);
115115

116116
return $this->getHttpClient()->post(
117117
sprintf('/api/servers/%s/files/create-directory', $this->server->uuid),
@@ -133,7 +133,7 @@ public function createDirectory(string $name, string $path): ResponseInterface
133133
*/
134134
public function renameFile(string $from, string $to): ResponseInterface
135135
{
136-
Assert::isInstanceOf(Server::class, $this->server);
136+
Assert::isInstanceOf($this->server, Server::class);
137137

138138
return $this->getHttpClient()->put(
139139
sprintf('/api/servers/%s/files/rename', $this->server->uuid),
@@ -154,7 +154,7 @@ public function renameFile(string $from, string $to): ResponseInterface
154154
*/
155155
public function copyFile(string $location): ResponseInterface
156156
{
157-
Assert::isInstanceOf(Server::class, $this->server);
157+
Assert::isInstanceOf($this->server, Server::class);
158158

159159
return $this->getHttpClient()->post(
160160
sprintf('/api/servers/%s/files/copy', $this->server->uuid),
@@ -174,7 +174,7 @@ public function copyFile(string $location): ResponseInterface
174174
*/
175175
public function deleteFile(string $location): ResponseInterface
176176
{
177-
Assert::isInstanceOf(Server::class, $this->server);
177+
Assert::isInstanceOf($this->server, Server::class);
178178

179179
return $this->getHttpClient()->post(
180180
sprintf('/api/servers/%s/files/delete', $this->server->uuid),

app/Repositories/Wings/DaemonRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function setNode(Node $node)
7171
*/
7272
public function getHttpClient(array $headers = []): Client
7373
{
74-
Assert::isInstanceOf(Node::class, $this->node);
74+
Assert::isInstanceOf($this->node, Node::class);
7575

7676
return new Client([
7777
'verify' => $this->app->environment('production'),

app/Repositories/Wings/DaemonServerRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DaemonServerRepository extends DaemonRepository
1717
*/
1818
public function getDetails(): array
1919
{
20-
Assert::isInstanceOf(Server::class, $this->server);
20+
Assert::isInstanceOf($this->server, Server::class);
2121

2222
try {
2323
$response = $this->getHttpClient()->get(

0 commit comments

Comments
 (0)