Skip to content

Commit

Permalink
Provide response body when exceptions are ignored
Browse files Browse the repository at this point in the history
Normally, exceptions are thrown on HTTP errors (404, 500, etc).
If the user chooses to ignore these exceptions with the `ignore`
parameter, the response body was entirely empty.

This commit returns the full response body from the server so that
the user can parse it as required.
  • Loading branch information
polyfractal committed Jan 21, 2014
1 parent 9770b23 commit c0983eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions src/Elasticsearch/Connections/CurlMultiConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,17 @@ private function process4xxError($method, $uri, $response)
$exceptionText = "$statusCode Server Exception: $exceptionText\n$responseBody";

if ($statusCode === 400 && strpos($responseBody, "AlreadyExpiredException") !== false) {
throw new AlreadyExpiredException($exceptionText, $statusCode);
throw new AlreadyExpiredException($responseBody, $statusCode);
} elseif ($statusCode === 403) {
throw new Forbidden403Exception($exceptionText, $statusCode);
throw new Forbidden403Exception($responseBody, $statusCode);
} elseif ($statusCode === 404) {
throw new Missing404Exception($exceptionText, $statusCode);
throw new Missing404Exception($responseBody, $statusCode);
} elseif ($statusCode === 409) {
throw new Conflict409Exception($exceptionText, $statusCode);
throw new Conflict409Exception($responseBody, $statusCode);
} elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
throw new ScriptLangNotSupportedException($exceptionText. $statusCode);
throw new ScriptLangNotSupportedException($responseBody. $statusCode);
} elseif ($statusCode === 400) {
throw new BadRequest400Exception($exceptionText, $statusCode);
throw new BadRequest400Exception($responseBody, $statusCode);
}
}

Expand All @@ -278,13 +278,13 @@ private function process5xxError($method, $uri, $response)
$this->log->error($exceptionText);

if ($statusCode === 500 && strpos($responseBody, "RoutingMissingException") !== false) {
throw new RoutingMissingException($exceptionText, $statusCode);
throw new RoutingMissingException($responseBody, $statusCode);
} elseif ($statusCode === 500 && preg_match('/ActionRequestValidationException.+ no documents to get/',$responseBody) === 1) {
throw new NoDocumentsToGetException($exceptionText, $statusCode);
throw new NoDocumentsToGetException($responseBody, $statusCode);
} elseif ($statusCode === 500 && strpos($responseBody, 'NoShardAvailableActionException') !== false) {
throw new NoShardAvailableException($exceptionText, $statusCode);
throw new NoShardAvailableException($responseBody, $statusCode);
} else {
throw new ServerErrorResponseException($exceptionText, $statusCode);
throw new ServerErrorResponseException($responseBody, $statusCode);
}


Expand Down
20 changes: 10 additions & 10 deletions src/Elasticsearch/Connections/GuzzleConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ private function process5xxError(Request $request, ServerErrorResponseException
$this->log->error($exceptionText);

if ($statusCode === 500 && strpos($responseBody, "RoutingMissingException") !== false) {
throw new RoutingMissingException($exceptionText, $statusCode, $exception);
throw new RoutingMissingException($responseBody, $statusCode, $exception);
} elseif ($statusCode === 500 && preg_match('/ActionRequestValidationException.+ no documents to get/',$responseBody) === 1) {
throw new NoDocumentsToGetException($exceptionText, $statusCode, $exception);
throw new NoDocumentsToGetException($responseBody, $statusCode, $exception);
} elseif ($statusCode === 500 && strpos($responseBody, 'NoShardAvailableActionException') !== false) {
throw new NoShardAvailableException($exceptionText, $statusCode, $exception);
throw new NoShardAvailableException($responseBody, $statusCode, $exception);
} else {
throw new \Elasticsearch\Common\Exceptions\ServerErrorResponseException($exceptionText, $statusCode, $exception);
throw new \Elasticsearch\Common\Exceptions\ServerErrorResponseException($responseBody, $statusCode, $exception);
}


Expand All @@ -226,17 +226,17 @@ private function process4xxError(Request $request, ClientErrorResponseException
$exceptionText = "$statusCode Server Exception: $exceptionText\n$responseBody";

if ($statusCode === 400 && strpos($responseBody, "AlreadyExpiredException") !== false) {
throw new AlreadyExpiredException($exceptionText, $statusCode, $exception);
throw new AlreadyExpiredException($responseBody, $statusCode, $exception);
} elseif ($statusCode === 403) {
throw new Forbidden403Exception($exceptionText, $statusCode, $exception);
throw new Forbidden403Exception($responseBody, $statusCode, $exception);
} elseif ($statusCode === 404) {
throw new Missing404Exception($exceptionText, $statusCode, $exception);
throw new Missing404Exception($responseBody, $statusCode, $exception);
} elseif ($statusCode === 409) {
throw new Conflict409Exception($exceptionText, $statusCode, $exception);
throw new Conflict409Exception($responseBody, $statusCode, $exception);
} elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
throw new ScriptLangNotSupportedException($exceptionText. $statusCode);
throw new ScriptLangNotSupportedException($responseBody. $statusCode);
} elseif ($statusCode === 400) {
throw new BadRequest400Exception($exceptionText, $statusCode, $exception);
throw new BadRequest400Exception($responseBody, $statusCode, $exception);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Elasticsearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function performRequest()
throw $exception;
} else {
//TODO return null or dedicated object here instead?
return array('data' => array());
return array('data' => $exception->getMessage());
}
}

Expand Down

0 comments on commit c0983eb

Please sign in to comment.