Skip to content

Commit 94aa242

Browse files
committed
Merge branch 'oogFranz-fix/977_2'
2 parents f2dd0b5 + 01ad2d3 commit 94aa242

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,15 @@ private function tryDeserializeError(array $response, string $errorClass): Elast
679679
{
680680
$error = $this->serializer->deserialize($response['body'], $response['transfer_stats']);
681681
if (is_array($error) === true) {
682+
if (isset($error['error']) === false) {
683+
// <2.0 "i just blew up" nonstructured exception
684+
// $error is an array but we don't know the format, reuse the response body instead
685+
// added json_encode to convert into a string
686+
return new $errorClass(json_encode($response['body']), (int) $response['status']);
687+
}
688+
682689
// 2.0 structured exceptions
683-
if (isset($error['error']['reason']) === true) {
690+
if (is_array($error['error']) && array_key_exists('reason', $error['error']) === true) {
684691
// Try to use root cause first (only grabs the first root cause)
685692
$root = $error['error']['root_cause'];
686693
if (isset($root) && isset($root[0])) {
@@ -694,18 +701,16 @@ private function tryDeserializeError(array $response, string $errorClass): Elast
694701
$original = new $errorClass(json_encode($response['body']), $response['status']);
695702

696703
return new $errorClass("$type: $cause", (int) $response['status'], $original);
697-
} elseif (isset($error['error']) === true) {
698-
// <2.0 semi-structured exceptions
699-
// added json_encode to convert into a string
700-
$original = new $errorClass(json_encode($response['body']), $response['status']);
701-
702-
return new $errorClass($error['error'], (int) $response['status'], $original);
703704
}
704-
705-
// <2.0 "i just blew up" nonstructured exception
706-
// $error is an array but we don't know the format, reuse the response body instead
705+
// <2.0 semi-structured exceptions
707706
// added json_encode to convert into a string
708-
return new $errorClass(json_encode($response['body']), (int) $response['status']);
707+
$original = new $errorClass(json_encode($response['body']), $response['status']);
708+
709+
$errorEncoded = $error['error'];
710+
if (is_array($errorEncoded)) {
711+
$errorEncoded = json_encode($errorEncoded);
712+
}
713+
return new $errorClass($errorEncoded, (int) $response['status'], $original);
709714
}
710715

711716
// if responseBody is not string, we convert it so it can be used as Exception message

tests/Elasticsearch/Tests/Connections/ConnectionTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use Elasticsearch\Client;
88
use Elasticsearch\ClientBuilder;
9+
use Elasticsearch\Common\Exceptions\ServerErrorResponseException;
910
use Elasticsearch\Connections\Connection;
1011
use Elasticsearch\Serializers\SerializerInterface;
12+
use Elasticsearch\Serializers\SmartSerializer;
1113
use Psr\Log\LoggerInterface;
14+
use ReflectionClass;
1215

1316
class ConnectionTest extends \PHPUnit\Framework\TestCase
1417
{
@@ -282,4 +285,36 @@ public function testGetHeadersContainBasicAuthOverHostArrayConfig()
282285
$this->assertArrayNotHasKey('Authorization', $request['headers']);
283286
$this->assertContains('username:password', $request['client']['curl'][CURLOPT_USERPWD]);
284287
}
288+
289+
public function testTryDeserializeErrorWithMasterNotDiscoveredException()
290+
{
291+
$host = [
292+
'host' => 'localhost'
293+
];
294+
295+
$connection = new Connection(
296+
function () {
297+
},
298+
$host,
299+
[],
300+
new SmartSerializer(),
301+
$this->logger,
302+
$this->trace
303+
);
304+
305+
$reflection = new ReflectionClass(Connection::class);
306+
$tryDeserializeError = $reflection->getMethod('tryDeserializeError');
307+
$tryDeserializeError->setAccessible(true);
308+
309+
$body = '{"error":{"root_cause":[{"type":"master_not_discovered_exception","reason":null}],"type":"master_not_discovered_exception","reason":null},"status":503}';
310+
$response = [
311+
'transfer_stats' => [],
312+
'status' => 503,
313+
'body' => $body
314+
];
315+
316+
$result = $tryDeserializeError->invoke($connection, $response, ServerErrorResponseException::class);
317+
$this->assertInstanceOf(ServerErrorResponseException::class, $result);
318+
$this->assertContains('master_not_discovered_exception', $result->getMessage());
319+
}
285320
}

0 commit comments

Comments
 (0)