Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning in Connection::tryDeserializeError() #168

Merged
Show file tree
Hide file tree
Changes from 2 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
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'];
dblock marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading