Skip to content
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

Show generic error messages when server returns no response #1056

Merged
merged 1 commit into from
Oct 5, 2020
Merged
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
30 changes: 22 additions & 8 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ private function buildCurlCommand(string $method, string $uri, ?string $body): s
private function process4xxError(array $request, array $response, array $ignore): ?ElasticsearchException
{
$statusCode = $response['status'];
$responseBody = $response['body'];

/**
* @var \Exception $exception
Expand All @@ -617,12 +616,8 @@ private function process4xxError(array $request, array $response, array $ignore)
if (array_search($response['status'], $ignore) !== false) {
return null;
}

// if responseBody is not string, we convert it so it can be used as Exception message
if (!is_string($responseBody)) {
$responseBody = json_encode($responseBody);
}


$responseBody = $this->convertBodyToString($response['body'], $statusCode, $exception);
if ($statusCode === 403) {
$exception = new Forbidden403Exception($responseBody, $statusCode);
} elseif ($statusCode === 404) {
Expand Down Expand Up @@ -667,14 +662,33 @@ private function process5xxError(array $request, array $response, array $ignore)
} elseif ($statusCode === 500 && strpos($responseBody, 'NoShardAvailableActionException') !== false) {
$exception = new NoShardAvailableException($exception->getMessage(), $statusCode, $exception);
} else {
$exception = new ServerErrorResponseException($responseBody, $statusCode);
$exception = new ServerErrorResponseException(
$this->convertBodyToString($responseBody, $statusCode, $exception),
$statusCode
);
}

$this->logRequestFail($request, $response, $exception);

throw $exception;
}

private function convertBodyToString($body, int $statusCode, ElasticsearchException $exception) : string
{
if (empty($body)) {
return sprintf(
"Unknown %d error from Elasticsearch %s",
$statusCode,
$exception->getMessage()
);
}
// if body is not string, we convert it so it can be used as Exception message
if (!is_string($body)) {
return json_encode($body);
}
return $body;
}

private function tryDeserialize400Error(array $response): ElasticsearchException
{
return $this->tryDeserializeError($response, BadRequest400Exception::class);
Expand Down