Skip to content

Commit

Permalink
Merge pull request #668 from einorler/issue648
Browse files Browse the repository at this point in the history
implemented throwing of exception in managers commit
  • Loading branch information
saimaz committed Aug 30, 2016
2 parents b13b4a6 + 50fcde4 commit 34a6fd6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -7,7 +7,7 @@ php:
- hhvm
env:
global:
- ES_VERSION="2.1.1"
- ES_VERSION="2.3.4"
- SYMFONY="~3.0"
matrix:
allow_failures:
Expand Down
9 changes: 9 additions & 0 deletions Service/Manager.php
Expand Up @@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\Service;

use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use ONGR\ElasticsearchBundle\Event\Events;
use ONGR\ElasticsearchBundle\Event\BulkEvent;
Expand Down Expand Up @@ -354,6 +355,8 @@ public function refresh(array $params = [])
* @param array $params Parameters that will be passed to the flush or refresh queries.
*
* @return null|array
*
* @throws ClientErrorResponseException
*/
public function commit(array $params = [])
{
Expand All @@ -369,6 +372,12 @@ public function commit(array $params = [])
$bulkResponse = $this->client->bulk($bulkQueries);
$this->stopwatch('stop', 'bulk');

if ($bulkResponse['errors']) {
throw new ClientErrorResponseException(
'An error occurred during the commit to elasticsearch'
);
}

$this->bulkQueries = [];
$this->bulkCount = 0;

Expand Down
18 changes: 18 additions & 0 deletions Tests/Functional/Service/ManagerTest.php
Expand Up @@ -358,4 +358,22 @@ public function testParseResultsException()
$search->addQuery(new MatchAllQuery());
$repo->execute($search, 'non_existant_type');
}

/**
* Tests the exception thrown by the commit method
*
* @expectedException \Elasticsearch\Common\Exceptions\ClientErrorResponseException
* @expectedExceptionMessage An error occurred during the commit to elasticsearch
*/
public function testCommitException()
{
$manager = $this->getManager();
$product = new Product();
$nestedProduct = new Product();
$nestedProduct->setTitle('test');
$product->setTitle($nestedProduct);

$manager->persist($product);
$manager->commit();
}
}
2 changes: 1 addition & 1 deletion Tests/Unit/Service/ManagerTest.php
Expand Up @@ -205,7 +205,7 @@ public function testBulkWithCommitModeSet()
$indices = $this->getMock('Elasticsearch\Namespaces\IndicesNamespace', [], [], '', false);

$esClient = $this->getMock('Elasticsearch\Client', [], [], '', false);
$esClient->expects($this->any())->method('bulk')->with($expected)->willReturn([]);
$esClient->expects($this->any())->method('bulk')->with($expected)->willReturn(['errors' => false]);
$esClient->expects($this->any())->method('indices')->will($this->returnValue($indices));

$metadataCollector = $this->getMockBuilder('ONGR\ElasticsearchBundle\Mapping\MetadataCollector')
Expand Down

0 comments on commit 34a6fd6

Please sign in to comment.