Skip to content

Commit a6d896b

Browse files
committed
Simplify error parsing now that we don't support <2.0 errors
We can safely assume all errors are structured, and if they aren't, just dump the body instead of trying to heuristically sort out the pre-2.0 semi-structured messages.
1 parent 864d4d3 commit a6d896b

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

src/Elasticsearch/Connections/Connection.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -667,36 +667,22 @@ 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) {
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);
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'];
692679
}
693680

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']);
681+
$original = new $errorClass($response['body'], $response['status']);
682+
return new $errorClass("$type: $cause", $response['status'], $original);
697683
}
698684

699-
// <2.0 "i just blew up" nonstructured exception
700-
return new $errorClass($error, $response['status']);
685+
// Response mangled or unexpected, just return the body
686+
return new $errorClass($error, $response['body']);
701687
}
702688
}

0 commit comments

Comments
 (0)