2017-10-06 00:16:22 -05:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2017-10-06 00:16:22 -05:00
namespace Pterodactyl\Exceptions\Http\Connection ;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Client\RequestException ;
use Illuminate\Http\Client\Response as ClientResponse ;
2018-01-30 20:36:59 -06:00
use Illuminate\Http\Response ;
2021-01-17 20:51:41 -08:00
use Illuminate\Support\Facades\Log ;
2017-10-06 00:16:22 -05:00
use Pterodactyl\Exceptions\DisplayException ;
2026-10-05 18:10:18 -04:00
use Throwable ;
2017-10-06 00:16:22 -05:00
2026-10-05 18:10:18 -04:00
/** @method Throwable getPrevious() */
2017-10-06 00:16:22 -05:00
class DaemonConnectionException extends DisplayException
{
2026-10-05 18:10:18 -04:00
private readonly int $statusCode ;
2020-08-13 21:21:08 -07:00
2026-10-05 18:10:18 -04:00
private readonly ? string $requestId ;
2020-08-13 21:21:08 -07:00
2026-10-05 18:10:18 -04:00
public function __construct ( Throwable $previous , bool $useStatusCode = true , ? ClientResponse $response = null )
{
$response ??= $previous instanceof RequestException ? $previous -> response : null ;
$this -> requestId = $response ?-> header ( 'X-Request-Id' );
$this -> statusCode = $this -> statusFor ( $response , $useStatusCode );
$level = $this -> statusCode >= 500 && $this -> statusCode !== Response :: HTTP_GATEWAY_TIMEOUT
? self :: LEVEL_ERROR
: self :: LEVEL_WARNING ;
2020-08-13 21:21:08 -07:00
2026-10-05 18:10:18 -04:00
parent :: __construct ( $this -> messageFor ( $response ), $previous , $level );
2017-10-06 00:16:22 -05:00
}
2017-12-31 10:30:19 -06:00
2026-10-05 18:10:18 -04:00
public function report () : void
2021-01-17 20:51:41 -08:00
{
2026-10-05 18:10:18 -04:00
Log :: log ( $this -> getErrorLevel (), $this -> getPrevious () -> getMessage (), [
2021-01-17 20:51:41 -08:00
'request_id' => $this -> requestId ,
2026-10-05 18:10:18 -04:00
'exception' => $this -> getPrevious (),
2021-01-17 20:51:41 -08:00
]);
}
2022-10-14 10:59:20 -06:00
public function getStatusCode () : int
2017-12-31 10:30:19 -06:00
{
return $this -> statusCode ;
}
2021-01-17 20:51:41 -08:00
2022-10-14 10:59:20 -06:00
public function getRequestId () : ? string
2021-01-17 20:51:41 -08:00
{
return $this -> requestId ;
}
2026-10-05 18:10:18 -04:00
private function statusFor ( ? ClientResponse $response , bool $useStatusCode ) : int
{
if ( ! $useStatusCode || ! $response instanceof ClientResponse ) {
return Response :: HTTP_GATEWAY_TIMEOUT ;
}
// A truncated or malformed successful response is still an upstream failure.
return $response -> status () < 400 ? Response :: HTTP_BAD_GATEWAY : $response -> status ();
}
private function messageFor ( ? ClientResponse $response ) : string
{
if ( ! $response instanceof ClientResponse ) {
return 'Could not establish a connection to the machine running this server. Please try again.' ;
}
$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 -> status (), $this -> requestId ?? '<nil>' );
if ( $this -> statusCode >= 500 ) {
return $message ;
}
$body = $response -> json ();
$remoteError = is_array ( $body ) && is_string ( $body [ 'error' ] ?? null ) ? $body [ 'error' ] : $message ;
return sprintf ( 'An error occurred on the remote host: %s. (request id: %s)' , $remoteError , $this -> requestId ?? '<nil>' );
}
2017-10-06 00:16:22 -05:00
}