Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ApiResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,7 +57,7 @@ public function getError() : ?string {
if(is_array($error)) {
return $this->getXml('body');
}
return $error;
return StringEx::stringify($error);
}
return null;
}
Expand Down
11 changes: 9 additions & 2 deletions tests/ApiResult/getError_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ public static function body_expected_dataProvider() : array {
[['error' => ['message' => 'foo']], 'foo'],
[['exception' => ['message' => 'bar']], 'bar'],
[['foo' => ['qux' => ['@id' => '123']]], '<foo><qux id="123"></qux></foo>'],
['<error>qux</error>', '<error>qux</error>']
['<error>qux</error>', '<error>qux</error>'],

// 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']
];
}

/**
* @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 = [
Expand Down