Path: blob/1.0-develop/app/Exceptions/Http/Connection/DaemonConnectionException.php
10284 views
<?php12namespace Pterodactyl\Exceptions\Http\Connection;34use Illuminate\Http\Response;5use Illuminate\Support\Facades\Log;6use GuzzleHttp\Exception\GuzzleException;7use Pterodactyl\Exceptions\DisplayException;89/**10* @method \GuzzleHttp\Exception\GuzzleException getPrevious()11*/12class DaemonConnectionException extends DisplayException13{14private int $statusCode = Response::HTTP_GATEWAY_TIMEOUT;1516/**17* Every request to the Wings instance will return a unique X-Request-Id header18* which allows for all errors to be efficiently tied to a specific request that19* triggered them, and gives users a more direct method of informing hosts when20* something goes wrong.21*/22private ?string $requestId;2324/**25* Throw a displayable exception caused by a daemon connection error.26*/27public function __construct(GuzzleException $previous, bool $useStatusCode = true)28{29/** @var \GuzzleHttp\Psr7\Response|null $response */30$response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null;31$this->requestId = $response?->getHeaderLine('X-Request-Id');3233if ($useStatusCode) {34$this->statusCode = is_null($response) ? $this->statusCode : $response->getStatusCode();35// There are rare conditions where wings encounters a panic condition and crashes the36// request being made after content has already been sent over the wire. In these cases37// you can end up with a "successful" response code that is actual an error.38//39// Handle those better here since we shouldn't ever end up in this exception state and40// be returning a 2XX level response.41if ($this->statusCode < 400) {42$this->statusCode = Response::HTTP_BAD_GATEWAY;43}44}4546if (is_null($response)) {47$message = 'Could not establish a connection to the machine running this server. Please try again.';48} else {49$message = sprintf('There was an error while communicating with the machine running this server. This error has been logged, please try again. (code: %s) (request_id: %s)', $response->getStatusCode(), $this->requestId ?? '<nil>');50}5152// Attempt to pull the actual error message off the response and return that if it is not53// a 500 level error.54if ($this->statusCode < 500 && !is_null($response)) {55$body = json_decode($response->getBody()->__toString(), true);56$message = sprintf('An error occurred on the remote host: %s. (request id: %s)', $body['error'] ?? $message, $this->requestId ?? '<nil>');57}5859$level = $this->statusCode >= 500 && $this->statusCode !== 50460? DisplayException::LEVEL_ERROR61: DisplayException::LEVEL_WARNING;6263parent::__construct($message, $previous, $level);64}6566/**67* Override the default reporting method for DisplayException by just logging immediately68* here and including the specific X-Request-Id header that was returned by the call.69*/70public function report()71{72Log::{$this->getErrorLevel()}($this->getPrevious(), [73'request_id' => $this->requestId,74]);75}7677/**78* Return the HTTP status code for this exception.79*/80public function getStatusCode(): int81{82return $this->statusCode;83}8485public function getRequestId(): ?string86{87return $this->requestId;88}89}909192