Skip to content

Commit 34a6fd6

Browse files
authored
Merge pull request #668 from einorler/issue648
implemented throwing of exception in managers commit
2 parents b13b4a6 + 50fcde4 commit 34a6fd6

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ php:
77
- hhvm
88
env:
99
global:
10-
- ES_VERSION="2.1.1"
10+
- ES_VERSION="2.3.4"
1111
- SYMFONY="~3.0"
1212
matrix:
1313
allow_failures:

Service/Manager.php

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace ONGR\ElasticsearchBundle\Service;
1313

1414
use Elasticsearch\Client;
15+
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
1516
use Elasticsearch\Common\Exceptions\Missing404Exception;
1617
use ONGR\ElasticsearchBundle\Event\Events;
1718
use ONGR\ElasticsearchBundle\Event\BulkEvent;
@@ -354,6 +355,8 @@ public function refresh(array $params = [])
354355
* @param array $params Parameters that will be passed to the flush or refresh queries.
355356
*
356357
* @return null|array
358+
*
359+
* @throws ClientErrorResponseException
357360
*/
358361
public function commit(array $params = [])
359362
{
@@ -369,6 +372,12 @@ public function commit(array $params = [])
369372
$bulkResponse = $this->client->bulk($bulkQueries);
370373
$this->stopwatch('stop', 'bulk');
371374

375+
if ($bulkResponse['errors']) {
376+
throw new ClientErrorResponseException(
377+
'An error occurred during the commit to elasticsearch'
378+
);
379+
}
380+
372381
$this->bulkQueries = [];
373382
$this->bulkCount = 0;
374383

Tests/Functional/Service/ManagerTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,22 @@ public function testParseResultsException()
358358
$search->addQuery(new MatchAllQuery());
359359
$repo->execute($search, 'non_existant_type');
360360
}
361+
362+
/**
363+
* Tests the exception thrown by the commit method
364+
*
365+
* @expectedException \Elasticsearch\Common\Exceptions\ClientErrorResponseException
366+
* @expectedExceptionMessage An error occurred during the commit to elasticsearch
367+
*/
368+
public function testCommitException()
369+
{
370+
$manager = $this->getManager();
371+
$product = new Product();
372+
$nestedProduct = new Product();
373+
$nestedProduct->setTitle('test');
374+
$product->setTitle($nestedProduct);
375+
376+
$manager->persist($product);
377+
$manager->commit();
378+
}
361379
}

Tests/Unit/Service/ManagerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function testBulkWithCommitModeSet()
205205
$indices = $this->getMock('Elasticsearch\Namespaces\IndicesNamespace', [], [], '', false);
206206

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

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

0 commit comments

Comments
 (0)