HTTP.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Exception based on HTTP response
  4. *
  5. * @package Requests
  6. */
  7. /**
  8. * Exception based on HTTP response
  9. *
  10. * @package Requests
  11. */
  12. class Requests_Exception_HTTP extends Requests_Exception {
  13. /**
  14. * HTTP status code
  15. *
  16. * @var integer
  17. */
  18. protected $code = 0;
  19. /**
  20. * Reason phrase
  21. *
  22. * @var string
  23. */
  24. protected $reason = 'Unknown';
  25. /**
  26. * Create a new exception
  27. *
  28. * There is no mechanism to pass in the status code, as this is set by the
  29. * subclass used. Reason phrases can vary, however.
  30. *
  31. * @param string|null $reason Reason phrase
  32. * @param mixed $data Associated data
  33. */
  34. public function __construct($reason = null, $data = null) {
  35. if ($reason !== null) {
  36. $this->reason = $reason;
  37. }
  38. $message = sprintf('%d %s', $this->code, $this->reason);
  39. parent::__construct($message, 'httpresponse', $data, $this->code);
  40. }
  41. /**
  42. * Get the status message
  43. */
  44. public function getReason() {
  45. return $this->reason;
  46. }
  47. /**
  48. * Get the correct exception class for a given error code
  49. *
  50. * @param int|bool $code HTTP status code, or false if unavailable
  51. * @return string Exception class name to use
  52. */
  53. public static function get_class($code) {
  54. if (!$code) {
  55. return 'Requests_Exception_HTTP_Unknown';
  56. }
  57. $class = sprintf('Requests_Exception_HTTP_%d', $code);
  58. if (class_exists($class)) {
  59. return $class;
  60. }
  61. return 'Requests_Exception_HTTP_Unknown';
  62. }
  63. }