Skip to content

Commit

Permalink
Fix warning in Connection::tryDeserializeError() (#168)
Browse files Browse the repository at this point in the history
* Fixed warning in Connection::tryDeserializeError().

Signed-off-by: Thomas Seidl <[email protected]>

* Cleaned up code.

Signed-off-by: Thomas Seidl <[email protected]>

* Added CHANGELOG.md entry.

Signed-off-by: Thomas Seidl <[email protected]>

---------

Signed-off-by: Thomas Seidl <[email protected]>
  • Loading branch information
drunken-monkey authored Mar 28, 2024
1 parent 4398973 commit 8962f02
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fixed host urls with trailing slash in the url ([#130](https://github.com/opensearch-project/opensearch-php/pull/140))
- Fixed point-in-time APIs ([#142](https://github.com/opensearch-project/opensearch-php/pull/142))
- Fixed bug in ClientBuilder where basic authentication is overridden by connection params ([#160](https://github.com/opensearch-project/opensearch-php/pull/160))
- Fixed PHP warning in `Connection::tryDeserializeError()` for some error responses ([#167](https://github.com/opensearch-project/opensearch-php/issues/167))

### Security

Expand Down
11 changes: 3 additions & 8 deletions src/OpenSearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,9 @@ private function tryDeserializeError(array $response, string $errorClass): OpenS
// 2.0 structured exceptions
if (is_array($error['error']) && array_key_exists('reason', $error['error']) === true) {
// Try to use root cause first (only grabs the first root cause)
$root = $error['error']['root_cause'];
if (isset($root) && isset($root[0])) {
$cause = $root[0]['reason'];
$type = $root[0]['type'];
} else {
$cause = $error['error']['reason'];
$type = $error['error']['type'];
}
$info = $error['error']['root_cause'][0] ?? $error['error'];
$cause = $info['reason'];
$type = $info['type'];
// added json_encode to convert into a string
$original = new $errorClass(json_encode($response['body']), $response['status']);

Expand Down
35 changes: 35 additions & 0 deletions tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,41 @@ function () {
$this->assertStringContainsString('master_not_discovered_exception', $result->getMessage());
}

/**
* @see https://github.com/opensearch-project/opensearch-php/issues/167
*/
public function testTryDeserializeErrorWith403Error()
{
$host = [
'host' => 'localhost'
];

$connection = new Connection(
function () {
},
$host,
[],
new SmartSerializer(),
$this->logger,
$this->trace
);

$reflection = new ReflectionClass(Connection::class);
$tryDeserializeError = $reflection->getMethod('tryDeserializeError');
$tryDeserializeError->setAccessible(true);

$body = '{"status":403,"error":{"reason":"403 Forbidden","type":"Forbidden"}}';
$response = [
'transfer_stats' => [],
'status' => 403,
'body' => $body
];

$result = $tryDeserializeError->invoke($connection, $response, ServerErrorResponseException::class);
$this->assertInstanceOf(ServerErrorResponseException::class, $result);
$this->assertStringContainsString('403 Forbidden', $result->getMessage());
}

public function testHeaderClientParamIsResetAfterSent()
{
$host = [
Expand Down

0 comments on commit 8962f02

Please sign in to comment.