Skip to content

Commit e215a0b

Browse files
committed
Added unit test to #977 fix + defensive approach
1 parent 84fedf0 commit e215a0b

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,6 @@ private function tryDeserializeError(array $response, string $errorClass): Elast
685685
// added json_encode to convert into a string
686686
return new $errorClass(json_encode($response['body']), (int) $response['status']);
687687
}
688-
689688
// 2.0 structured exceptions
690689
if (is_array($error['error']) && array_key_exists('reason', $error['error']) === true) {
691690
// Try to use root cause first (only grabs the first root cause)
@@ -699,14 +698,31 @@ private function tryDeserializeError(array $response, string $errorClass): Elast
699698
}
700699
// added json_encode to convert into a string
701700
$original = new $errorClass(json_encode($response['body']), $response['status']);
702-
703701
return new $errorClass("$type: $cause", (int) $response['status'], $original);
704702
}
705703
// <2.0 semi-structured exceptions
706704
// added json_encode to convert into a string
707705
$original = new $errorClass(json_encode($response['body']), $response['status']);
708-
709706
return new $errorClass($error['error'], (int) $response['status'], $original);
707+
708+
// // 2.0 structured exceptions
709+
// if (is_array($error['error'])) {
710+
// // Try to use root cause first (only grabs the first root cause)
711+
// $root = $error['error']['root_cause'];
712+
// if (isset($root) && isset($root[0])) {
713+
// $cause = $root[0]['reason'];
714+
// $type = $root[0]['type'];
715+
// } else {
716+
// $cause = $error['error']['reason'];
717+
// $type = $error['error']['type'];
718+
// }
719+
// }
720+
// // added json_encode to convert into a string
721+
// $original = new $errorClass(json_encode($response['body']), $response['status']);
722+
723+
// return isset($cause) && isset($type) ?
724+
// new $errorClass("$type: $cause", (int) $response['status'], $original) :
725+
// new $errorClass(json_encode($error['error']), (int) $response['status'], $original);
710726
}
711727

712728
// 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)