Beispiel #1
0
 /**
  * Create a new exception
  *
  * If `$data` is an instance of {@see Requests_Response}, uses the status
  * code from it. Otherwise, sets as 0
  *
  * @param string|null $reason Reason phrase
  * @param mixed $data Associated data
  */
 public function __construct($reason = null, $data = null)
 {
     if ($data instanceof Requests_Response) {
         $this->code = $data->status_code;
     }
     parent::__construct($reason, $data);
 }
Beispiel #2
0
 /**
  * Throws an exception if the request was not successful
  *
  * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
  * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
  * @param boolean $allow_redirects Set to false to throw on a 3xx as well
  */
 public function throw_for_status($allow_redirects = true)
 {
     if ($this->status_code >= 300 && $this->status_code < 400) {
         if (!$allow_redirects) {
             throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
         }
     } elseif (!$this->success) {
         $exception = Requests_Exception_HTTP::get_class($this->status_code);
         throw new $exception(null, $this);
     }
 }