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