Skip to content

Commit 675e780

Browse files
committed
Fix test failures
1 parent 38075c6 commit 675e780

File tree

4 files changed

+99
-102
lines changed

4 files changed

+99
-102
lines changed

app/Exceptions/DisplayException.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
class DisplayException extends PterodactylException
1616
{
17+
const LEVEL_WARNING = 'warning';
18+
const LEVEL_ERROR = 'error';
19+
1720
/**
1821
* @var string
1922
*/
@@ -27,7 +30,7 @@ class DisplayException extends PterodactylException
2730
* @param string $level
2831
* @internal param mixed $log
2932
*/
30-
public function __construct($message, Throwable $previous = null, $level = 'error')
33+
public function __construct($message, Throwable $previous = null, $level = self::LEVEL_ERROR)
3134
{
3235
$this->level = $level;
3336

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Exceptions\Http\Connection;
11+
12+
use GuzzleHttp\Exception\GuzzleException;
13+
use Pterodactyl\Exceptions\DisplayException;
14+
15+
class DaemonConnectionException extends DisplayException
16+
{
17+
/**
18+
* Throw a displayable exception caused by a daemon connection error.
19+
*
20+
* @param \GuzzleHttp\Exception\GuzzleException $previous
21+
*/
22+
public function __construct(GuzzleException $previous)
23+
{
24+
/** @var \GuzzleHttp\Psr7\Response|null $response */
25+
$response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null;
26+
27+
parent::__construct(trans('admin/server.exceptions.daemon_exception', [
28+
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
29+
]), $previous, DisplayException::LEVEL_WARNING);
30+
}
31+
}

app/Services/Servers/ServerCreationService.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
namespace Pterodactyl\Services\Servers;
1111

1212
use Ramsey\Uuid\Uuid;
13-
use Illuminate\Log\Writer;
14-
use Illuminate\Database\DatabaseManager;
1513
use GuzzleHttp\Exception\RequestException;
16-
use Pterodactyl\Exceptions\DisplayException;
14+
use Illuminate\Database\ConnectionInterface;
1715
use Pterodactyl\Services\Nodes\NodeCreationService;
1816
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
1917
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
2018
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
2119
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
20+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2221
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
2322
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
2423

@@ -35,14 +34,14 @@ class ServerCreationService
3534
protected $configurationStructureService;
3635

3736
/**
38-
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
37+
* @var \Illuminate\Database\ConnectionInterface
3938
*/
40-
protected $daemonServerRepository;
39+
protected $connection;
4140

4241
/**
43-
* @var \Illuminate\Database\DatabaseManager
42+
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
4443
*/
45-
protected $database;
44+
protected $daemonServerRepository;
4645

4746
/**
4847
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
@@ -74,50 +73,42 @@ class ServerCreationService
7473
*/
7574
protected $validatorService;
7675

77-
/**
78-
* @var \Illuminate\Log\Writer
79-
*/
80-
protected $writer;
81-
8276
/**
8377
* CreationService constructor.
8478
*
8579
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
80+
* @param \Illuminate\Database\ConnectionInterface $connection
8681
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
87-
* @param \Illuminate\Database\DatabaseManager $database
8882
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
8983
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
9084
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
9185
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
9286
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
9387
* @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService
9488
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
95-
* @param \Illuminate\Log\Writer $writer
9689
*/
9790
public function __construct(
9891
AllocationRepositoryInterface $allocationRepository,
92+
ConnectionInterface $connection,
9993
DaemonServerRepositoryInterface $daemonServerRepository,
100-
DatabaseManager $database,
10194
NodeRepositoryInterface $nodeRepository,
10295
ServerConfigurationStructureService $configurationStructureService,
10396
ServerRepositoryInterface $repository,
10497
ServerVariableRepositoryInterface $serverVariableRepository,
10598
UserRepositoryInterface $userRepository,
10699
UsernameGenerationService $usernameService,
107-
VariableValidatorService $validatorService,
108-
Writer $writer
100+
VariableValidatorService $validatorService
109101
) {
110102
$this->allocationRepository = $allocationRepository;
111-
$this->daemonServerRepository = $daemonServerRepository;
112103
$this->configurationStructureService = $configurationStructureService;
113-
$this->database = $database;
104+
$this->connection = $connection;
105+
$this->daemonServerRepository = $daemonServerRepository;
114106
$this->nodeRepository = $nodeRepository;
115107
$this->repository = $repository;
116108
$this->serverVariableRepository = $serverVariableRepository;
117109
$this->userRepository = $userRepository;
118110
$this->usernameService = $usernameService;
119111
$this->validatorService = $validatorService;
120-
$this->writer = $writer;
121112
}
122113

123114
/**
@@ -136,7 +127,7 @@ public function create(array $data)
136127
$validator = $this->validatorService->isAdmin()->setFields($data['environment'])->validate($data['option_id']);
137128
$uniqueShort = str_random(8);
138129

139-
$this->database->beginTransaction();
130+
$this->connection->beginTransaction();
140131

141132
$server = $this->repository->create([
142133
'uuid' => Uuid::uuid4()->toString(),
@@ -187,16 +178,13 @@ public function create(array $data)
187178

188179
// Create the server on the daemon & commit it to the database.
189180
try {
190-
$this->daemonServerRepository->setNode($server->node_id)->create($structure, ['start_on_completion' => (bool) $data['start_on_completion']]);
191-
$this->database->commit();
181+
$this->daemonServerRepository->setNode($server->node_id)->create($structure, [
182+
'start_on_completion' => (bool) array_get($data, 'start_on_completion', false),
183+
]);
184+
$this->connection->commit();
192185
} catch (RequestException $exception) {
193-
$response = $exception->getResponse();
194-
$this->writer->warning($exception);
195-
$this->database->rollBack();
196-
197-
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
198-
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
199-
]));
186+
$this->connection->rollBack();
187+
throw new DaemonConnectionException($exception);
200188
}
201189

202190
return $server;

0 commit comments

Comments
 (0)