From fd385388c8158d0c4822ad9dd3497e17da21dc89 Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Wed, 2 Nov 2016 07:39:02 -0400 Subject: [PATCH] Revert "Simplify error parsing now that we don't support <2.0 errors" This reverts commit a6d896bcfddb59e7725d000445d32da17cdf9b82. --- src/Elasticsearch/Connections/Connection.php | 38 +++++++++++++------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Elasticsearch/Connections/Connection.php b/src/Elasticsearch/Connections/Connection.php index 3de7899a5..0b008adbf 100644 --- a/src/Elasticsearch/Connections/Connection.php +++ b/src/Elasticsearch/Connections/Connection.php @@ -667,22 +667,36 @@ private function tryDeserialize500Error($response) private function tryDeserializeError($response, $errorClass) { $error = $this->serializer->deserialize($response['body'], $response['transfer_stats']); - if (is_array($error) === true && isset($error['error']['root_cause']) === 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']; + if (is_array($error) === true) { + // 2.0 structured exceptions + if (isset($error['error']['reason']) === 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']; + } + + $original = new $errorClass($response['body'], $response['status']); + + return new $errorClass("$type: $cause", $response['status'], $original); + } elseif (isset($error['error']) === true) { + // <2.0 semi-structured exceptions + $original = new $errorClass($response['body'], $response['status']); + + return new $errorClass($error['error'], $response['status'], $original); } - $original = new $errorClass($response['body'], $response['status']); - return new $errorClass("$type: $cause", $response['status'], $original); + // <2.0 "i just blew up" nonstructured exception + // $error is an array but we don't know the format, reuse the response body instead + return new $errorClass($response['body'], $response['status']); } - // Response mangled or unexpected, just return the body + // <2.0 "i just blew up" nonstructured exception return new $errorClass($response['body']); } }