Skip to content

Commit fd38538

Browse files
committed
Revert "Simplify error parsing now that we don't support <2.0 errors"
This reverts commit a6d896b.
1 parent 90fbd53 commit fd38538

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -667,22 +667,36 @@ private function tryDeserialize500Error($response)
667667
private function tryDeserializeError($response, $errorClass)
668668
{
669669
$error = $this->serializer->deserialize($response['body'], $response['transfer_stats']);
670-
if (is_array($error) === true && isset($error['error']['root_cause']) === true) {
671-
// Try to use root cause first (only grabs the first root cause)
672-
$root = $error['error']['root_cause'];
673-
if (isset($root) && isset($root[0])) {
674-
$cause = $root[0]['reason'];
675-
$type = $root[0]['type'];
676-
} else {
677-
$cause = $error['error']['reason'];
678-
$type = $error['error']['type'];
670+
if (is_array($error) === true) {
671+
// 2.0 structured exceptions
672+
if (isset($error['error']['reason']) === true) {
673+
674+
// Try to use root cause first (only grabs the first root cause)
675+
$root = $error['error']['root_cause'];
676+
if (isset($root) && isset($root[0])) {
677+
$cause = $root[0]['reason'];
678+
$type = $root[0]['type'];
679+
} else {
680+
$cause = $error['error']['reason'];
681+
$type = $error['error']['type'];
682+
}
683+
684+
$original = new $errorClass($response['body'], $response['status']);
685+
686+
return new $errorClass("$type: $cause", $response['status'], $original);
687+
} elseif (isset($error['error']) === true) {
688+
// <2.0 semi-structured exceptions
689+
$original = new $errorClass($response['body'], $response['status']);
690+
691+
return new $errorClass($error['error'], $response['status'], $original);
679692
}
680693

681-
$original = new $errorClass($response['body'], $response['status']);
682-
return new $errorClass("$type: $cause", $response['status'], $original);
694+
// <2.0 "i just blew up" nonstructured exception
695+
// $error is an array but we don't know the format, reuse the response body instead
696+
return new $errorClass($response['body'], $response['status']);
683697
}
684698

685-
// Response mangled or unexpected, just return the body
699+
// <2.0 "i just blew up" nonstructured exception
686700
return new $errorClass($response['body']);
687701
}
688702
}

0 commit comments

Comments
 (0)