Example #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 $reason Reason phrase
  * @param mixed $data Associated data
  */
 public function __construct($reason = null, $data = null)
 {
     if ($data instanceof RequestsResponse) {
         $this->code = $data->status_code;
     }
     parent::__construct($reason, $data);
 }
 /**
  * Throws an exception if the request was not successful
  *
  * @throws RequestsException If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
  * @throws RequestsExceptionHTTP On non-successful status code. Exception class corresponds to code (e.g. {@see RequestsExceptionHTTP404})
  * @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 RequestsException('Redirection not allowed', 'response.no_redirects', $this);
         }
     } elseif (!$this->success) {
         $exception = RequestsExceptionHTTP::get_class($this->status_code);
         throw new $exception(null, $this);
     }
 }