2017-10-06 05:16:22 +00:00
< ? php
namespace Pterodactyl\Exceptions\Http\Connection ;
2018-01-31 02:36:59 +00:00
use Illuminate\Http\Response ;
2021-01-18 04:51:41 +00:00
use Illuminate\Support\Facades\Log ;
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
{
2022-10-14 16:59:20 +00:00
private int $statusCode = Response :: HTTP_GATEWAY_TIMEOUT ;
2017-12-31 16:30:19 +00:00
2021-01-18 04:51:41 +00:00
/**
* Every request to the Wings instance will return a unique X - Request - Id header
* which allows for all errors to be efficiently tied to a specific request that
* triggered them , and gives users a more direct method of informing hosts when
* something goes wrong .
*/
2022-10-14 16:59:20 +00:00
private ? string $requestId ;
2021-01-18 04:51:41 +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 ;
2022-10-14 16:59:20 +00:00
$this -> requestId = $response ? -> getHeaderLine ( 'X-Request-Id' );
2017-10-06 05:16:22 +00:00
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 ();
2021-04-04 17:45:33 +00:00
// There are rare conditions where wings encounters a panic condition and crashes the
// request being made after content has already been sent over the wire. In these cases
// you can end up with a "successful" response code that is actual an error.
//
// Handle those better here since we shouldn't ever end up in this exception state and
// be returning a 2XX level response.
if ( $this -> statusCode < 400 ) {
$this -> statusCode = Response :: HTTP_BAD_GATEWAY ;
}
2017-12-31 16:30:19 +00:00
}
2021-01-18 04:51:41 +00:00
if ( is_null ( $response )) {
$message = 'Could not establish a connection to the machine running this server. Please try again.' ;
} else {
$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>' );
}
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-26 03:20:51 +00:00
if ( $this -> statusCode < 500 && ! is_null ( $response )) {
2021-01-18 04:51:41 +00:00
$body = json_decode ( $response -> getBody () -> __toString (), true );
2021-01-26 03:20:51 +00:00
$message = sprintf ( 'An error occurred on the remote host: %s. (request id: %s)' , $body [ 'error' ] ? ? $message , $this -> requestId ? ? '<nil>' );
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
2021-01-18 04:51:41 +00:00
/**
* Override the default reporting method for DisplayException by just logging immediately
* here and including the specific X - Request - Id header that was returned by the call .
*/
public function report ()
{
Log :: { $this -> getErrorLevel ()}( $this -> getPrevious (), [
'request_id' => $this -> requestId ,
]);
}
2017-12-31 16:30:19 +00:00
/**
* Return the HTTP status code for this exception .
*/
2022-10-14 16:59:20 +00:00
public function getStatusCode () : int
2017-12-31 16:30:19 +00:00
{
return $this -> statusCode ;
}
2021-01-18 04:51:41 +00:00
2022-10-14 16:59:20 +00:00
public function getRequestId () : ? string
2021-01-18 04:51:41 +00:00
{
return $this -> requestId ;
}
2017-10-06 05:16:22 +00:00
}