Skip to content

Commit

Permalink
Fixed warning in Connection::tryDeserializeError().
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Seidl <[email protected]>
  • Loading branch information
drunken-monkey committed Nov 18, 2023
1 parent 9ebb183 commit 6ada309
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/OpenSearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ 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])) {
$root = $error['error']['root_cause'] ?? NULL;
if (isset($root[0])) {
$cause = $root[0]['reason'];
$type = $root[0]['type'];
} else {
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 6ada309

Please sign in to comment.