Skip to content

Fix #977: Only treat structured exception after 2.0. #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,42 +679,27 @@ private function tryDeserializeError(array $response, string $errorClass): Elast
{
$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'];
}
// added json_encode to convert into a string
$original = new $errorClass(json_encode($response['body']), $response['status']);

return new $errorClass("$type: $cause", (int) $response['status'], $original);
} elseif (isset($error['error']) === true) {
// <2.0 semi-structured exceptions
// added json_encode to convert into a string
$original = new $errorClass(json_encode($response['body']), $response['status']);

return new $errorClass($error['error'], (int) $response['status'], $original);
$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
// added json_encode to convert into a string
return new $errorClass(json_encode($response['body']), (int) $response['status']);
$original = new $errorClass(json_encode($response['body']), $response['status']);

return new $errorClass("$type: $cause", (int) $response['status'], $original);
}

// Response mangled or unexpected, just return the body
// if responseBody is not string, we convert it so it can be used as Exception message
$responseBody = $response['body'];
if (!is_string($responseBody)) {
$responseBody = json_encode($responseBody);
}

// <2.0 "i just blew up" nonstructured exception
return new $errorClass($responseBody);
}
}