Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Httpful/Exception/ConnectionErrorException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
<?php
<?php

namespace Httpful\Exception;

class ConnectionErrorException extends \Exception
class ConnectionErrorException extends \Exception
{
}
public $curl_object = null;
public function __construct($message, $code = 0, Exception $previous = null, $curl_object = null) {
$this->curl_object = $curl_object;
parent::__construct($message, $code, $previous);
}

public function getCurlObject() {
return $this->curl_object;
}

public function wasTimeout() {
return $this->code == CURLE_OPERATION_TIMEOUTED;
}
}
2 changes: 1 addition & 1 deletion src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function send()

if ($result === false) {
$this->_error(curl_error($this->_ch));
throw new ConnectionErrorException('Unable to connect.');
throw new ConnectionErrorException('Unable to connect.',curl_errno($this->_ch),null,$this->_ch);
}

$info = curl_getinfo($this->_ch);
Expand Down
16 changes: 16 additions & 0 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
use Httpful\Http;
use Httpful\Response;
use Httpful\Handlers\JsonHandler;
use Httpful\Exception\ConnectionErrorException;

class HttpfulTest extends \PHPUnit_Framework_TestCase
{
const TEST_SERVER = '127.0.0.1:8008';
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 @@ -503,6 +505,20 @@ public function testHasProxyWithProxy() {
$this->assertTrue($r->hasProxy());
}

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

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

Expand Down