Skip to content

Commit

Permalink
Backport fix for exception messages. (#1418)
Browse files Browse the repository at this point in the history
A fix was made in 3.x branch for exception messages to stop an un-useful
error `Error: Wrong parameters for Exception([string $exception [, long $code [, Exception $previous
  = NULL]]])` appearing. This commit backports that same fix into the
2.x branch.
  • Loading branch information
jenkoian authored and ruflin committed Jan 28, 2018
1 parent 0df1d7b commit d72afd0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
9 changes: 2 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ All notable changes to this project will be documented in this file based on the

## [Unreleased](https://github.com/ruflin/Elastica/compare/2.3.1...HEAD)

### Backward Compatibility Breaks

### Bugfixes

### Added

### Improvements

### Deprecated
- Introduce getFullError() for ResponseSet
- Use the above to improve the output of getError() on Response


## [2.3.1](https://github.com/ruflin/Elastica/releases/tag/2.3.1) - 2015-10-17
Expand Down
19 changes: 19 additions & 0 deletions lib/Elastica/Bulk/ResponseSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ public function getError()
return $error;
}

/**
* Returns first found error (full array).
*
* @return array|string
*/
public function getFullError()
{
$error = '';

foreach ($this->getBulkResponses() as $bulkResponse) {
if ($bulkResponse->hasError()) {
$error = $bulkResponse->getFullError();
break;
}
}

return $error;
}

/**
* @return bool
*/
Expand Down
35 changes: 32 additions & 3 deletions lib/Elastica/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,43 @@ public function __construct($responseString, $responseStatus = null)
*/
public function getError()
{
$message = '';
$error = $this->getFullError();
if (!$error) {
return '';
}

if (is_string($error)) {
return $error;
}

if (isset($error['root_cause'][0])) {
$error = $error['root_cause'][0];
}

$message = $error['reason'];
if (isset($error['index'])) {
$message .= ' [index: '.$error['index'].']';
}

return $message;
}

/**
* A keyed array representing any errors that occured.
*
* In case of http://localhost:9200/_alias/test the error is a string
*
* @return array|string Error data
*/
public function getFullError()
{
$response = $this->getData();

if (isset($response['error'])) {
$message = $response['error'];
return $response['error'];
}

return $message;
return;
}

/**
Expand Down

0 comments on commit d72afd0

Please sign in to comment.