Skip to content

Commit e504c75

Browse files
authored
Merge pull request #677 from saimaz/patch-exception
Added custom exception for bulk with error body in message
2 parents 34a6fd6 + 97cceb3 commit e504c75

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

Exception/BulkWithErrorsException.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Exception;
13+
14+
class BulkWithErrorsException extends \Exception
15+
{
16+
/**
17+
* @var array
18+
*/
19+
protected $response;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function __construct($message = '', $code = 0, \Exception $previous = null, $response = [])
25+
{
26+
parent::__construct($message, $code, $previous);
27+
$this->response = $response;
28+
}
29+
30+
/**
31+
* @return array
32+
*/
33+
public function getResponse()
34+
{
35+
return $this->response;
36+
}
37+
}

Service/Manager.php

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

1414
use Elasticsearch\Client;
15-
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
1615
use Elasticsearch\Common\Exceptions\Missing404Exception;
1716
use ONGR\ElasticsearchBundle\Event\Events;
1817
use ONGR\ElasticsearchBundle\Event\BulkEvent;
19-
use ONGR\ElasticsearchBundle\Event\PersistEvent;
2018
use ONGR\ElasticsearchBundle\Event\CommitEvent;
19+
use ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException;
2120
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
2221
use ONGR\ElasticsearchBundle\Result\AbstractResultsIterator;
2322
use ONGR\ElasticsearchBundle\Result\Converter;
@@ -356,7 +355,7 @@ public function refresh(array $params = [])
356355
*
357356
* @return null|array
358357
*
359-
* @throws ClientErrorResponseException
358+
* @throws BulkWithErrorsException
360359
*/
361360
public function commit(array $params = [])
362361
{
@@ -373,8 +372,11 @@ public function commit(array $params = [])
373372
$this->stopwatch('stop', 'bulk');
374373

375374
if ($bulkResponse['errors']) {
376-
throw new ClientErrorResponseException(
377-
'An error occurred during the commit to elasticsearch'
375+
throw new BulkWithErrorsException(
376+
json_encode($bulkResponse),
377+
0,
378+
null,
379+
$bulkResponse
378380
);
379381
}
380382

Tests/Functional/Service/ManagerTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@ public function testParseResultsException()
362362
/**
363363
* Tests the exception thrown by the commit method
364364
*
365-
* @expectedException \Elasticsearch\Common\Exceptions\ClientErrorResponseException
366-
* @expectedExceptionMessage An error occurred during the commit to elasticsearch
365+
* @expectedException \ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException
367366
*/
368367
public function testCommitException()
369368
{

0 commit comments

Comments
 (0)