ResponseException.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Elastica\Exception;
  3. use Elastica\Request;
  4. use Elastica\Response;
  5. /**
  6. * Response exception.
  7. *
  8. * @author Nicolas Ruflin <spam@ruflin.com>
  9. */
  10. class ResponseException extends \RuntimeException implements ExceptionInterface
  11. {
  12. /**
  13. * @var \Elastica\Request Request object
  14. */
  15. protected $_request;
  16. /**
  17. * @var \Elastica\Response Response object
  18. */
  19. protected $_response;
  20. /**
  21. * Construct Exception.
  22. *
  23. * @param \Elastica\Request $request
  24. * @param \Elastica\Response $response
  25. */
  26. public function __construct(Request $request, Response $response)
  27. {
  28. $this->_request = $request;
  29. $this->_response = $response;
  30. parent::__construct($response->getErrorMessage());
  31. }
  32. /**
  33. * Returns request object.
  34. *
  35. * @return \Elastica\Request Request object
  36. */
  37. public function getRequest()
  38. {
  39. return $this->_request;
  40. }
  41. /**
  42. * Returns response object.
  43. *
  44. * @return \Elastica\Response Response object
  45. */
  46. public function getResponse()
  47. {
  48. return $this->_response;
  49. }
  50. /**
  51. * Returns elasticsearch exception.
  52. *
  53. * @return ElasticsearchException
  54. */
  55. public function getElasticsearchException()
  56. {
  57. $response = $this->getResponse();
  58. return new ElasticsearchException($response->getStatus(), $response->getErrorMessage());
  59. }
  60. }