diff --git a/src/ApiResult.php b/src/ApiResult.php
index 4a5e274..0360017 100644
--- a/src/ApiResult.php
+++ b/src/ApiResult.php
@@ -36,13 +36,13 @@ public function getError() : ?string {
// formatted API error
$error = $this->getVal('body/error/message');
if($error !== null) {
- return $error;
+ return StringEx::stringify($error);
}
// exception API error
$error = $this->getVal('body/exception/message');
if($error !== null) {
- return $error;
+ return StringEx::stringify($error);
}
// curl error
@@ -57,7 +57,7 @@ public function getError() : ?string {
if(is_array($error)) {
return $this->getXml('body');
}
- return $error;
+ return StringEx::stringify($error);
}
return null;
}
diff --git a/tests/ApiResult/getError_Test.php b/tests/ApiResult/getError_Test.php
index 6feaa29..32bd30b 100644
--- a/tests/ApiResult/getError_Test.php
+++ b/tests/ApiResult/getError_Test.php
@@ -28,7 +28,14 @@ public static function body_expected_dataProvider() : array {
[['error' => ['message' => 'foo']], 'foo'],
[['exception' => ['message' => 'bar']], 'bar'],
[['foo' => ['qux' => ['@id' => '123']]], ''],
- ['qux', 'qux']
+ ['qux', 'qux'],
+
+ // non-string scalars deserialized from the API response body must be coerced to
+ // string, not returned as-is: getError() is declared ": ?string" and under
+ // strict_types this throws a TypeError instead of returning a value
+ [['error' => ['message' => true]], '1'],
+ [['exception' => ['message' => 42]], '42'],
+ [3.14, '3.14']
];
}
@@ -36,7 +43,7 @@ public static function body_expected_dataProvider() : array {
* @dataProvider body_expected_dataProvider
* @test
*/
- public function Can_get_error_from_body(string|array $body, string $expected) {
+ public function Can_get_error_from_body(string|array|bool|int|float $body, string $expected) {
// arrange
$data = [