Skip to content

Commit

Permalink
Merge branch 'mlambley-null-error-messages-7x' into 7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Oct 5, 2020
2 parents 78172a7 + 2c213f8 commit d86e1f8
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Elasticsearch\Common\Exceptions\TransportException;
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\Transport;
use Exception;
use GuzzleHttp\Ring\Core;
use GuzzleHttp\Ring\Exception\ConnectException;
use GuzzleHttp\Ring\Exception\RingException;
Expand Down Expand Up @@ -612,7 +613,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 @@ -622,12 +622,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 @@ -672,14 +668,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, Exception $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

0 comments on commit d86e1f8

Please sign in to comment.