Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): build the message for an error body that nests arrays

Yudhi Armyndharis4d96123d2724
diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.php
index c3d430079..2cdc66599 100644
--- a/sdk/php/src/Http/HttpExecutor.php
+++ b/sdk/php/src/Http/HttpExecutor.php
@@ -188,7 +188,12 @@ class HttpExecutor
$envelope = is_array($data) && isset($data['statusCode'], $data['message']) ? $data : null;
$rawMessage = $envelope['message'] ?? $data;
if (is_array($rawMessage)) {
- $messageText = implode(', ', array_map('strval', $rawMessage));
+ // A body without the envelope (the readiness 503's {status, details}) can nest arrays,
+ // which strval() cannot convert; render those as JSON.
+ $messageText = implode(', ', array_map(
+ fn ($v) => is_array($v) ? json_encode($v) : (string) $v,
+ $rawMessage,
+ ));
} elseif (is_string($rawMessage)) {
$messageText = $rawMessage;
} else {
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index 0bc65984e..a3d3c30a9 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -7,6 +7,7 @@ namespace OpenWA\Tests;
use OpenWA\Exceptions\OpenWAApiException;
use OpenWA\Exceptions\OpenWAAuthException;
use OpenWA\Exceptions\OpenWANotFoundException;
+use OpenWA\Exceptions\OpenWAServiceUnavailableException;
use OpenWA\Exceptions\OpenWATimeoutException;
use PHPUnit\Framework\TestCase;
@@ -150,6 +151,23 @@ class ClientTest extends TestCase
}
}
+ public function testNonEnvelopeErrorBodyMapsToTypedException(): void
+ {
+ // The readiness probe answers 503 with {status, details}: no statusCode/message, and a
+ // nested array that strval() cannot convert.
+ $backend = (new MockBackend())->on(503, [
+ 'status' => 'error',
+ 'details' => ['mainDatabase' => ['status' => 'down']],
+ ]);
+ try {
+ $backend->makeClient()->health->ready();
+ $this->fail('Expected exception');
+ } catch (OpenWAServiceUnavailableException $e) {
+ $this->assertSame(503, $e->getStatus());
+ $this->assertStringContainsString('{"mainDatabase":{"status":"down"}}', $e->getMessage());
+ }
+ }
+
public function testTimeoutWithErrno28MapsToTimeoutException(): void
{
// Regression guard: cURL error 28 (CURLE_OPERATION_TIMEDOUT) must map to