Skip to content

Added custom exception for bulk with error body in message #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Exception/BulkWithErrorsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Exception;

class BulkWithErrorsException extends \Exception
{
/**
* @var array
*/
protected $response;

/**
* {@inheritdoc}
*/
public function __construct($message = '', $code = 0, \Exception $previous = null, $response = [])
{
parent::__construct($message, $code, $previous);
$this->response = $response;
}

/**
* @return array
*/
public function getResponse()
{
return $this->response;
}
}
12 changes: 7 additions & 5 deletions Service/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
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;
use ONGR\ElasticsearchBundle\Event\PersistEvent;
use ONGR\ElasticsearchBundle\Event\CommitEvent;
use ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException;
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
use ONGR\ElasticsearchBundle\Result\AbstractResultsIterator;
use ONGR\ElasticsearchBundle\Result\Converter;
Expand Down Expand Up @@ -356,7 +355,7 @@ public function refresh(array $params = [])
*
* @return null|array
*
* @throws ClientErrorResponseException
* @throws BulkWithErrorsException
*/
public function commit(array $params = [])
{
Expand All @@ -373,8 +372,11 @@ public function commit(array $params = [])
$this->stopwatch('stop', 'bulk');

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

Expand Down
3 changes: 1 addition & 2 deletions Tests/Functional/Service/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,7 @@ public function testParseResultsException()
/**
* Tests the exception thrown by the commit method
*
* @expectedException \Elasticsearch\Common\Exceptions\ClientErrorResponseException
* @expectedExceptionMessage An error occurred during the commit to elasticsearch
* @expectedException \ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException
*/
public function testCommitException()
{
Expand Down