Skip to content

Commit

Permalink
[+]: "Give more information to the Exception object to enable better …
Browse files Browse the repository at this point in the history
…error handling"

Thx @	jarretth

-> nategood#117
  • Loading branch information
voku committed Apr 30, 2016
1 parent 06c8d31 commit bef5cbf
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/Httpful/Exception/ConnectionErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class ConnectionErrorException extends \Exception
{
/**
* @var null|resource
*/
public $curl_object = null;

/**
* @var int
*/
Expand All @@ -19,6 +24,29 @@ class ConnectionErrorException extends \Exception
*/
private $curlErrorString;

/**
* ConnectionErrorException constructor.
*
* @param string $message
* @param int $code
* @param \Exception|null $previous
* @param null $curl_object
*/
public function __construct($message, $code = 0, \Exception $previous = null, $curl_object = null)
{
$this->curl_object = $curl_object;

parent::__construct($message, $code, $previous);
}

/**
* @return null|resource
*/
public function getCurlObject()
{
return $this->curl_object;
}

/**
* @return string
*/
Expand Down Expand Up @@ -58,4 +86,12 @@ public function setCurlErrorString($curlErrorString)

return $this;
}

/**
* @return bool
*/
public function wasTimeout()
{
return $this->code === CURLE_OPERATION_TIMEOUTED;
}
}
17 changes: 15 additions & 2 deletions src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,20 @@ class Request
/**
* Template Request object
*
* @var
* @var Request
*/
private static $_template;

/**
* @var int The maximum amount of data to retrieve.
*/
protected $download_limit;

/**
* @var string The data retrieved by the CURL request. Used only a download limit is set.
*/
protected $retrieved_data;

/**
* We made the constructor protected to force the factory style. This was
* done to keep the syntax cleaner and better the support the idea of
Expand Down Expand Up @@ -1527,7 +1537,10 @@ public function buildResponse($result)
$this->_error($curlErrorString);

$exception = new ConnectionErrorException(
'Unable to connect to "' . $this->uri . '": ' . $curlErrorNumber . ' ' . $curlErrorString
'Unable to connect to "' . $this->uri . '": ' . $curlErrorNumber . ' ' . $curlErrorString,
$curlErrorNumber,
null,
$this->_ch
);

$exception->setCurlErrorNumber($curlErrorNumber)->setCurlErrorString($curlErrorString);
Expand Down
18 changes: 18 additions & 0 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class HttpfulTest extends \PHPUnit_Framework_TestCase
const TEST_SERVER = TEST_SERVER;
const TEST_URL = 'http://127.0.0.1:8008';
const TEST_URL_400 = 'http://127.0.0.1:8008/400';
const TIMEOUT_URI = 'http://www.google.com:81';

const SAMPLE_JSON_HEADER =
"HTTP/1.1 200 OK
Expand Down Expand Up @@ -599,6 +600,23 @@ public function testHasProxyWithEnvironmentProxy()
self::assertTrue($r->hasProxy());
}

public function testTimeout()
{
try {
Request::init()
->uri(self::TIMEOUT_URI)
->timeout(1)
->send();
} catch (ConnectionErrorException $e) {
self::assertTrue(is_resource($e->getCurlObject()));
self::assertTrue($e->wasTimeout());

return;
}

self::assertFalse(true);
}

public function testParseJSON()
{
$handler = new JsonHandler();
Expand Down

0 comments on commit bef5cbf

Please sign in to comment.