HttpException.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Elastica\Exception\Connection;
  3. use Elastica\Exception\ConnectionException;
  4. use Elastica\Request;
  5. use Elastica\Response;
  6. /**
  7. * Connection exception.
  8. *
  9. * @author Nicolas Ruflin <spam@ruflin.com>
  10. */
  11. class HttpException extends ConnectionException
  12. {
  13. /**
  14. * Error code / message.
  15. *
  16. * @var int|string Error code / message
  17. */
  18. protected $_error = 0;
  19. /**
  20. * Construct Exception.
  21. *
  22. * @param int|string $error Error
  23. * @param \Elastica\Request $request
  24. * @param \Elastica\Response $response
  25. */
  26. public function __construct($error, Request $request = null, Response $response = null)
  27. {
  28. $this->_error = $error;
  29. $message = $this->getErrorMessage($this->getError());
  30. parent::__construct($message, $request, $response);
  31. }
  32. /**
  33. * Returns the error message corresponding to the error code
  34. * cUrl error code reference can be found here {@link http://curl.haxx.se/libcurl/c/libcurl-errors.html}.
  35. *
  36. * @param string $error Error code
  37. *
  38. * @return string Error message
  39. */
  40. public function getErrorMessage($error)
  41. {
  42. switch ($error) {
  43. case CURLE_UNSUPPORTED_PROTOCOL:
  44. return 'Unsupported protocol';
  45. case CURLE_FAILED_INIT:
  46. return 'Internal cUrl error?';
  47. case CURLE_URL_MALFORMAT:
  48. return 'Malformed URL';
  49. case CURLE_COULDNT_RESOLVE_PROXY:
  50. return "Couldn't resolve proxy";
  51. case CURLE_COULDNT_RESOLVE_HOST:
  52. return "Couldn't resolve host";
  53. case CURLE_COULDNT_CONNECT:
  54. return "Couldn't connect to host, Elasticsearch down?";
  55. case 28:
  56. return 'Operation timed out';
  57. }
  58. return 'Unknown error:'.$error;
  59. }
  60. /**
  61. * Return Error code / message.
  62. *
  63. * @return string Error code / message
  64. */
  65. public function getError()
  66. {
  67. return $this->_error;
  68. }
  69. }