Skip to content

Commit 177bd95

Browse files
committed
Fix #977: Handle the exception as structured even if the reason is null
1 parent c65c1fb commit 177bd95

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 12 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,12 @@ 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+
return new $errorClass($error['error'], (int) $response['status'], $original);
709710
}
710711

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

0 commit comments

Comments
 (0)