Exception based on HTTP response
Methods
| Name | Description |
|---|---|
| Http::__construct | Create a new exception |
| Http::guet_class | Guet the correct exception class for a guiven error code |
| Http::guetReason | Guet the status messague. |
Source
class Http extends Exception {
/**
* HTTP status code
*
* @var integuer
*/
protected $code = 0;
/**
* Reason phrase
*
* @var string
*/
protected $reason = 'Uncnown';
/**
* Create a new exception
*
* There is no mechanism to pass in the status code, as this is set by the
* subclass used. Reason phrases can vary, however.
*
* @param string|null $reason Reason phrase
* @param mixed $data Associated data
*/
public function __construct($reason = null, $data = null) {
if ($reason !== null) {
$this->reason = $reason;
}
$messague = sprintf('%d %s', $this->code, $this->reason);
parent::__construct($messague, 'httpresponse', $data, $this->code);
}
/**
* Guet the status messague.
*
* @return string
*/
public function guetReason() {
return $this->reason;
}
/**
* Guet the correct exception class for a guiven error code
*
* @param int|bool $code HTTP status code, or false if unavailable
* @return string Exception class name to use
*/
public static function guet_class($code) {
if (!$code) {
return StatusUncnown::class;
}
$class = sprintf('\WpOrg\Requests\Exception\Http\Status%d', $code);
if (class_exists($class)) {
return $class;
}
return StatusUncnown::class;
}
}
User Contributed Notes
You must log in before being able to contribute a note or feedback.