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() { public function pingBridge() {
try { $response = $this->bridge->pingBridge();
$bridgePingResponse = $this->bridge->pingBridge(); if ($this->wp->isWpError($response)) {
} catch (\Exception $e) { /** @var \WP_Error $response */
$errorDesc = $this->getErrorDescriptionByCode(Bridge::CHECK_ERROR_UNKNOWN);
return $this->errorResponse([ 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([ return $this->errorResponse([
APIError::UNKNOWN => $this->getErrorDescriptionByCode($code), APIError::UNKNOWN => $this->getErrorDescriptionByCode($code),
]); ]);

View File

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