2017-10-06 05:16:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Exceptions\Http\Connection;
|
|
|
|
|
2020-08-14 04:21:08 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2018-01-31 02:36:59 +00:00
|
|
|
use Illuminate\Http\Response;
|
2017-10-06 05:16:22 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
|
2020-05-10 02:19:45 +00:00
|
|
|
/**
|
|
|
|
* @method \GuzzleHttp\Exception\GuzzleException getPrevious()
|
|
|
|
*/
|
2017-10-06 05:16:22 +00:00
|
|
|
class DaemonConnectionException extends DisplayException
|
|
|
|
{
|
2017-12-31 16:30:19 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2018-01-31 02:36:59 +00:00
|
|
|
private $statusCode = Response::HTTP_GATEWAY_TIMEOUT;
|
2017-12-31 16:30:19 +00:00
|
|
|
|
2017-10-06 05:16:22 +00:00
|
|
|
/**
|
|
|
|
* Throw a displayable exception caused by a daemon connection error.
|
|
|
|
*/
|
2020-07-18 17:23:28 +00:00
|
|
|
public function __construct(GuzzleException $previous, bool $useStatusCode = true)
|
2017-10-06 05:16:22 +00:00
|
|
|
{
|
|
|
|
/** @var \GuzzleHttp\Psr7\Response|null $response */
|
|
|
|
$response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null;
|
|
|
|
|
2017-12-31 16:30:19 +00:00
|
|
|
if ($useStatusCode) {
|
2020-08-14 04:21:08 +00:00
|
|
|
$this->statusCode = is_null($response) ? $this->statusCode : $response->getStatusCode();
|
2017-12-31 16:30:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 04:21:08 +00:00
|
|
|
$message = trans('admin/server.exceptions.daemon_exception', [
|
2017-10-06 05:16:22 +00:00
|
|
|
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
|
2020-08-14 04:21:08 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Attempt to pull the actual error message off the response and return that if it is not
|
|
|
|
// a 500 level error.
|
2021-01-23 20:33:34 +00:00
|
|
|
if ($this->statusCode < 500 && !is_null($response)) {
|
2020-08-14 04:21:08 +00:00
|
|
|
$body = $response->getBody();
|
|
|
|
if (is_string($body) || (is_object($body) && method_exists($body, '__toString'))) {
|
|
|
|
$body = json_decode(is_string($body) ? $body : $body->__toString(), true);
|
2021-01-23 20:09:16 +00:00
|
|
|
$message = '[Wings Error]: ' . Arr::get($body, 'error', $message);
|
2020-08-14 04:21:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$level = $this->statusCode >= 500 && $this->statusCode !== 504
|
|
|
|
? DisplayException::LEVEL_ERROR
|
|
|
|
: DisplayException::LEVEL_WARNING;
|
|
|
|
|
|
|
|
parent::__construct($message, $previous, $level);
|
2017-10-06 05:16:22 +00:00
|
|
|
}
|
2017-12-31 16:30:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the HTTP status code for this exception.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getStatusCode()
|
|
|
|
{
|
|
|
|
return $this->statusCode;
|
|
|
|
}
|
2017-10-06 05:16:22 +00:00
|
|
|
}
|