From 41a7a2075837320bc9bd3bca4150e05a1ec9a115 Mon Sep 17 00:00:00 2001 From: ruflin Date: Wed, 4 Nov 2015 14:47:52 +0100 Subject: [PATCH] Fix exception tests with new error object --- CHANGELOG.md | 2 ++ Makefile | 2 +- .../Exception/ElasticsearchException.php | 20 ++++++++++++++++--- lib/Elastica/Exception/ResponseException.php | 4 ++-- lib/Elastica/Response.php | 19 +++++++++++++----- .../Test/Exception/ResponseExceptionTest.php | 9 ++++++--- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad63187aad..92d85a06aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,12 @@ All notable changes to this project will be documented in this file based on the - Remove ansible scripts for tests setup and Vagrantfile as not needed anymore. All is based on docker contaienrs now - Support for PHP 5.3 removed +- Elastica\Reponse::getError() now returns and array instead of a string ### Bugfixes ### Added +- Elastica\Reponse::getErrorMessage was added as getError is now an object ### Improvements - Travis builds were moved to docker-compose setup. Ansible scripts and Vagrant files were removed diff --git a/Makefile b/Makefile index e680d59406..9c0226005d 100644 --- a/Makefile +++ b/Makefile @@ -184,4 +184,4 @@ tmp: make elastica-image make setup mkdir -p build - docker-compose run elastica phpunit -c test/ test/lib/Elastica/Test/ClientTest.php + docker-compose run elastica phpunit -c test/ test/lib/Elastica/Test/Exception diff --git a/lib/Elastica/Exception/ElasticsearchException.php b/lib/Elastica/Exception/ElasticsearchException.php index 59cca0c689..83156041a9 100644 --- a/lib/Elastica/Exception/ElasticsearchException.php +++ b/lib/Elastica/Exception/ElasticsearchException.php @@ -20,16 +20,23 @@ class ElasticsearchException extends \Exception implements ExceptionInterface */ private $_isRemote = false; + /** + * @var array Error array + */ + protected $_error = array(); + /** * Constructs elasticsearch exception. * * @param int $code Error code - * @param string $error Error message from elasticsearch + * @param array $error Error object from elasticsearch */ public function __construct($code, $error) { - $this->_parseError($error); - parent::__construct($error, $code); + $this->_error = $error; + // TODO: es2 improve as now an array + $this->_parseError(json_encode($error)); + parent::__construct(json_encode($error), $code); } /** @@ -88,4 +95,11 @@ public function isRemoteTransportException() { return $this->_isRemote; } + + /** + * @return array Error array + */ + public function getError() { + return $this->_error; + } } diff --git a/lib/Elastica/Exception/ResponseException.php b/lib/Elastica/Exception/ResponseException.php index 5750230730..cfa245f0bc 100644 --- a/lib/Elastica/Exception/ResponseException.php +++ b/lib/Elastica/Exception/ResponseException.php @@ -31,7 +31,7 @@ public function __construct(Request $request, Response $response) { $this->_request = $request; $this->_response = $response; - parent::__construct($response->getError()); + parent::__construct($response->getErrorMessage()); } /** @@ -65,6 +65,6 @@ public function getElasticsearchException() $transfer = $response->getTransferInfo(); $code = array_key_exists('http_code', $transfer) ? $transfer['http_code'] : 0; - return new ElasticsearchException($code, $response->getError()); + return new ElasticsearchException($code, $response->getErrorMessage()); } } diff --git a/lib/Elastica/Response.php b/lib/Elastica/Response.php index ec5416473e..9db4346809 100644 --- a/lib/Elastica/Response.php +++ b/lib/Elastica/Response.php @@ -74,19 +74,28 @@ public function __construct($responseString, $responseStatus = null) /** * Error message. * - * @return string Error message + * @return array Error object */ public function getError() { - $message = ''; + $error = array(); $response = $this->getData(); if (isset($response['error'])) { - $message = $response['error']; + $error = $response['error']; } + return $error; + } + + /** + * @return string Error string based on the error object + */ + public function getErrorMessage() { + $error = $this->getError(); + + $message = ""; - // Encoding needed as much more information inside now - if (!is_string($message)) { + if (!is_string($error)) { $message = json_encode($message); } diff --git a/test/lib/Elastica/Test/Exception/ResponseExceptionTest.php b/test/lib/Elastica/Test/Exception/ResponseExceptionTest.php index 6fc975e76a..67bffc3a77 100644 --- a/test/lib/Elastica/Test/Exception/ResponseExceptionTest.php +++ b/test/lib/Elastica/Test/Exception/ResponseExceptionTest.php @@ -17,7 +17,8 @@ public function testCreateExistingIndex() $this->_createIndex('woo', false); $this->fail('Index created when it should fail'); } catch (ResponseException $ex) { - $this->assertEquals('IndexAlreadyExistsException', $ex->getElasticsearchException()->getExceptionName()); + $error = $ex->getResponse()->getError(); + $this->assertEquals('index_already_exists_exception', $error['type']); $this->assertEquals(400, $ex->getElasticsearchException()->getCode()); } } @@ -42,7 +43,8 @@ public function testBadType() ))); $this->fail('Indexing with wrong type should fail'); } catch (ResponseException $ex) { - $this->assertEquals('MapperParsingException', $ex->getElasticsearchException()->getExceptionName()); + $error = $ex->getResponse()->getError(); + $this->assertEquals('mapper_parsing_exception', $error['type']); $this->assertEquals(400, $ex->getElasticsearchException()->getCode()); } } @@ -58,7 +60,8 @@ public function testWhatever() try { $index->search(); } catch (ResponseException $ex) { - $this->assertEquals('IndexMissingException', $ex->getElasticsearchException()->getExceptionName()); + $error = $ex->getResponse()->getError(); + $this->assertEquals('index_not_found_exception', $error['type']); $this->assertEquals(404, $ex->getElasticsearchException()->getCode()); } }