Skip to content

Commit

Permalink
Simplify error parsing now that we don't support <2.0 errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
polyfractal committed Oct 17, 2016
1 parent 864d4d3 commit a6d896b
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions src/Elasticsearch/Connections/Connection.php
Expand Up @@ -667,36 +667,22 @@ private function tryDeserialize500Error($response)
private function tryDeserializeError($response, $errorClass)
{
$error = $this->serializer->deserialize($response['body'], $response['transfer_stats']);
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);
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'];
}

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

// <2.0 "i just blew up" nonstructured exception
return new $errorClass($error, $response['status']);
// Response mangled or unexpected, just return the body
return new $errorClass($error, $response['body']);
}
}

0 comments on commit a6d896b

Please sign in to comment.