Skip to content

Commit 575eab9

Browse files
committed
Less obtuse error messaging, include the request ID in the output
1 parent 187df97 commit 575eab9

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

app/Exceptions/Http/Connection/DaemonConnectionException.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Pterodactyl\Exceptions\Http\Connection;
44

5-
use Illuminate\Support\Arr;
65
use Illuminate\Http\Response;
6+
use Illuminate\Support\Facades\Log;
77
use GuzzleHttp\Exception\GuzzleException;
88
use Pterodactyl\Exceptions\DisplayException;
99

@@ -17,6 +17,16 @@ class DaemonConnectionException extends DisplayException
1717
*/
1818
private $statusCode = Response::HTTP_GATEWAY_TIMEOUT;
1919

20+
/**
21+
* Every request to the Wings instance will return a unique X-Request-Id header
22+
* which allows for all errors to be efficiently tied to a specific request that
23+
* triggered them, and gives users a more direct method of informing hosts when
24+
* something goes wrong.
25+
*
26+
* @var string|null
27+
*/
28+
private $requestId;
29+
2030
/**
2131
* Throw a displayable exception caused by a daemon connection error.
2232
*
@@ -27,23 +37,23 @@ public function __construct(GuzzleException $previous, bool $useStatusCode = tru
2737
{
2838
/** @var \GuzzleHttp\Psr7\Response|null $response */
2939
$response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null;
40+
$this->requestId = $response ? $response->getHeaderLine('X-Request-Id') : null;
3041

3142
if ($useStatusCode) {
3243
$this->statusCode = is_null($response) ? $this->statusCode : $response->getStatusCode();
3344
}
3445

35-
$message = trans('admin/server.exceptions.daemon_exception', [
36-
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
37-
]);
46+
if (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+
}
3851

3952
// Attempt to pull the actual error message off the response and return that if it is not
4053
// a 500 level error.
4154
if ($this->statusCode < 500 && ! is_null($response)) {
42-
$body = $response->getBody();
43-
if (is_string($body) || (is_object($body) && method_exists($body, '__toString'))) {
44-
$body = json_decode(is_string($body) ? $body : $body->__toString(), true);
45-
$message = "[Wings Error]: " . Arr::get($body, 'error', $message);
46-
}
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>');
4757
}
4858

4959
$level = $this->statusCode >= 500 && $this->statusCode !== 504
@@ -53,6 +63,19 @@ public function __construct(GuzzleException $previous, bool $useStatusCode = tru
5363
parent::__construct($message, $previous, $level);
5464
}
5565

66+
/**
67+
* Override the default reporting method for DisplayException by just logging immediately
68+
* here and including the specific X-Request-Id header that was returned by the call.
69+
*
70+
* @return void
71+
*/
72+
public function report()
73+
{
74+
Log::{$this->getErrorLevel()}($this->getPrevious(), [
75+
'request_id' => $this->requestId,
76+
]);
77+
}
78+
5679
/**
5780
* Return the HTTP status code for this exception.
5881
*
@@ -62,4 +85,12 @@ public function getStatusCode()
6285
{
6386
return $this->statusCode;
6487
}
88+
89+
/**
90+
* @return string|null
91+
*/
92+
public function getRequestId()
93+
{
94+
return $this->requestId;
95+
}
6596
}

resources/lang/en/admin/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
'no_new_default_allocation' => 'You are attempting to delete the default allocation for this server but there is no fallback allocation to use.',
1313
'marked_as_failed' => 'This server was marked as having failed a previous installation. Current status cannot be toggled in this state.',
1414
'bad_variable' => 'There was a validation error with the :name variable.',
15-
'daemon_exception' => 'There was an exception while attempting to communicate with the daemon resulting in a HTTP/:code response code. This exception has been logged.',
15+
'daemon_exception' => 'There was an exception while attempting to communicate with the daemon resulting in a HTTP/:code response code. This exception has been logged. (request id: :request_id)',
1616
'default_allocation_not_found' => 'The requested default allocation was not found in this server\'s allocations.',
1717
],
1818
'alerts' => [

0 commit comments

Comments
 (0)