ResponseException.php 2.2 KB

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