Fix Bridge ping API action

A follow-up fix for the adjustment made to Bridge ping in "Expand System Info report with MSS connection status" (9b0b43f).

Note that the logic was already flawed prior to the changes introduced in this PR: "pingBridge" of "Bridge" class would never throw an exception.

[MAILPOET-6302]
This commit is contained in:
Mustapha Hadid
2024-12-11 17:15:27 +03:00
committed by Aschepikov
parent ce44e07c59
commit adc91d5451
2 changed files with 20 additions and 14 deletions

View File

@@ -298,15 +298,17 @@ class Services extends APIEndpoint {
}
public function pingBridge() {
try {
$bridgePingResponse = $this->bridge->pingBridge();
} catch (\Exception $e) {
$response = $this->bridge->pingBridge();
if ($this->wp->isWpError($response)) {
/** @var \WP_Error $response */
$errorDesc = $this->getErrorDescriptionByCode(Bridge::CHECK_ERROR_UNKNOWN);
return $this->errorResponse([
APIError::UNKNOWN => $e->getMessage(),
APIError::UNKNOWN => "{$errorDesc}: {$response->get_error_message()}",
]);
}
if (!$this->bridge->validateBridgePingResponse($bridgePingResponse)) {
$code = $bridgePingResponse ?: Bridge::CHECK_ERROR_UNKNOWN;
if (!$this->bridge->validateBridgePingResponse($response)) {
$code = $this->wp->wpRemoteRetrieveResponseCode($response) ?: Bridge::CHECK_ERROR_UNKNOWN;
return $this->errorResponse([
APIError::UNKNOWN => $this->getErrorDescriptionByCode($code),
]);

View File

@@ -616,14 +616,12 @@ class ServicesTest extends \MailPoetTest {
);
}
public function testItReturnsErrorIfBridgePingThrowsException() {
$errorMessage = 'some error';
public function testItReturnsErrorIfBridgePingReturnWPError() {
$errorMessage = 'cURL error 6: Could not resolve host: https://bridge.mailpoet.com';
$bridge = $this->make(
new Bridge(),
[
'pingBridge' => function () use ($errorMessage) {
throw new \Exception($errorMessage);
},
'pingBridge' => new \WP_Error('error', $errorMessage),
]
);
$servicesEndpoint = $this->createServicesEndpointWithMocks(['bridge' => $bridge]);
@@ -631,21 +629,25 @@ class ServicesTest extends \MailPoetTest {
$response = $response->getData();
verify($response['errors'][0])->isArray();
verify($response['errors'][0]['message'])->stringContainsString($errorMessage);
verify($response['errors'][0]['message'])->stringContainsString('Contact your hosting support');
verify($response['errors'][0]['error'])->stringContainsString('unknown');
}
public function testItReturnsErrorIfBridgePingResultIsUnsuccessful() {
$errorCode = 500;
$bridge = $this->make(
new Bridge(),
[
'pingBridge' => false,
'pingBridge' => [
'response' => ['code' => $errorCode],
],
]
);
$servicesEndpoint = $this->createServicesEndpointWithMocks(['bridge' => $bridge]);
$response = $servicesEndpoint->pingBridge();
$response = $response->getData();
verify($response['errors'][0])->isArray();
verify($response['errors'][0]['message'])->stringContainsString('Contact your hosting support');
verify($response['errors'][0]['message'])->stringContainsString('code: 500');
verify($response['errors'][0]['error'])->stringContainsString('unknown');
}
@@ -653,7 +655,9 @@ class ServicesTest extends \MailPoetTest {
$bridge = $this->make(
new Bridge(),
[
'pingBridge' => 200, // HTTP OK
'pingBridge' => [
'response' => ['code' => 200],
],
]
);
$servicesEndpoint = $this->createServicesEndpointWithMocks(['bridge' => $bridge]);