1<?php 2 3namespace Elastica\Exception\Bulk; 4 5use Elastica\Bulk\ResponseSet; 6use Elastica\Exception\Bulk\Response\ActionException; 7use Elastica\Exception\BulkException; 8 9/** 10 * Bulk Response exception. 11 */ 12class ResponseException extends BulkException 13{ 14 /** 15 * @var ResponseSet ResponseSet object 16 */ 17 protected $_responseSet; 18 19 /** 20 * @var ActionException[] 21 */ 22 protected $_actionExceptions = []; 23 24 /** 25 * Construct Exception. 26 */ 27 public function __construct(ResponseSet $responseSet) 28 { 29 $this->_init($responseSet); 30 31 $message = 'Error in one or more bulk request actions:'.\PHP_EOL.\PHP_EOL; 32 $message .= $this->getActionExceptionsAsString(); 33 34 parent::__construct($message); 35 } 36 37 /** 38 * Returns bulk response set object. 39 */ 40 public function getResponseSet(): ResponseSet 41 { 42 return $this->_responseSet; 43 } 44 45 /** 46 * Returns array of failed actions. 47 * 48 * @return string[] Array of failed actions 49 */ 50 public function getFailures(): array 51 { 52 $errors = []; 53 54 foreach ($this->getActionExceptions() as $actionException) { 55 $errors[] = $actionException->getMessage(); 56 } 57 58 return $errors; 59 } 60 61 /** 62 * @return ActionException[] 63 */ 64 public function getActionExceptions(): array 65 { 66 return $this->_actionExceptions; 67 } 68 69 public function getActionExceptionsAsString(): string 70 { 71 $message = ''; 72 foreach ($this->getActionExceptions() as $actionException) { 73 $message .= $actionException->getMessage().\PHP_EOL; 74 } 75 76 return $message; 77 } 78 79 protected function _init(ResponseSet $responseSet): void 80 { 81 $this->_responseSet = $responseSet; 82 83 foreach ($responseSet->getBulkResponses() as $bulkResponse) { 84 if ($bulkResponse->hasError()) { 85 $this->_actionExceptions[] = new ActionException($bulkResponse); 86 } 87 } 88 } 89} 90