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 * @param ResponseSet $responseSet 28 */ 29 public function __construct(ResponseSet $responseSet) 30 { 31 $this->_init($responseSet); 32 33 $message = 'Error in one or more bulk request actions:'.PHP_EOL.PHP_EOL; 34 $message .= $this->getActionExceptionsAsString(); 35 36 parent::__construct($message); 37 } 38 39 /** 40 * @param ResponseSet $responseSet 41 */ 42 protected function _init(ResponseSet $responseSet) 43 { 44 $this->_responseSet = $responseSet; 45 46 foreach ($responseSet->getBulkResponses() as $bulkResponse) { 47 if ($bulkResponse->hasError()) { 48 $this->_actionExceptions[] = new ActionException($bulkResponse); 49 } 50 } 51 } 52 53 /** 54 * Returns bulk response set object. 55 * 56 * @return ResponseSet 57 */ 58 public function getResponseSet(): ResponseSet 59 { 60 return $this->_responseSet; 61 } 62 63 /** 64 * Returns array of failed actions. 65 * 66 * @return string[] Array of failed actions 67 */ 68 public function getFailures(): array 69 { 70 $errors = []; 71 72 foreach ($this->getActionExceptions() as $actionException) { 73 $errors[] = $actionException->getMessage(); 74 } 75 76 return $errors; 77 } 78 79 /** 80 * @return ActionException[] 81 */ 82 public function getActionExceptions(): array 83 { 84 return $this->_actionExceptions; 85 } 86 87 /** 88 * @return string 89 */ 90 public function getActionExceptionsAsString(): string 91 { 92 $message = ''; 93 foreach ($this->getActionExceptions() as $actionException) { 94 $message .= $actionException->getMessage().PHP_EOL; 95 } 96 97 return $message; 98 } 99} 100