<?php
namespace Elastica\Exception\Bulk;
use Elastica\Bulk\ResponseSet;
use Elastica\Exception\Bulk\Response\ActionException;
use Elastica\Exception\BulkException;
class ResponseException extends BulkException
{
protected $_responseSet;
protected $_actionExceptions = [];
public function __construct(ResponseSet $responseSet)
{
$this->_init($responseSet);
$message = 'Error in one or more bulk request actions:'.PHP_EOL.PHP_EOL;
$message .= $this->getActionExceptionsAsString();
parent::__construct($message);
}
protected function _init(ResponseSet $responseSet)
{
$this->_responseSet = $responseSet;
foreach ($responseSet->getBulkResponses() as $bulkResponse) {
if ($bulkResponse->hasError()) {
$this->_actionExceptions[] = new ActionException($bulkResponse);
}
}
}
public function getResponseSet()
{
return $this->_responseSet;
}
public function getFailures()
{
$errors = [];
foreach ($this->getActionExceptions() as $actionException) {
$errors[] = $actionException->getMessage();
}
return $errors;
}
public function getActionExceptions()
{
return $this->_actionExceptions;
}
public function getActionExceptionsAsString()
{
$message = '';
foreach ($this->getActionExceptions() as $actionException) {
$message .= $actionException->getMessage().PHP_EOL;
}
return $message;
}
}