Skip to content

Commit d1111f8

Browse files
committed
Throw a CurlException if we have a cURL error number.
1 parent d60a07c commit d1111f8

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/Kobas/Client.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kobas;
44

55

6+
use Kobas\Exception\CurlException;
67
use Kobas\Exception\HttpException;
78
use Kobas\Auth\Signer;
89
use Kobas\Request\Curl;
@@ -100,6 +101,7 @@ public function disableSSLVerification()
100101
* @param array $headers
101102
* @return mixed
102103
* @throws HttpException
104+
* @throws CurlException
103105
*/
104106
public function get($route, array $params = array(), array $headers = array())
105107
{
@@ -113,6 +115,7 @@ public function get($route, array $params = array(), array $headers = array())
113115
* @param array $headers
114116
* @return mixed
115117
* @throws HttpException
118+
* @throws CurlException
116119
*/
117120
public function post($route, array $params = array(), array $headers = array())
118121
{
@@ -126,6 +129,7 @@ public function post($route, array $params = array(), array $headers = array())
126129
* @param array $headers
127130
* @return mixed
128131
* @throws HttpException
132+
* @throws CurlException
129133
*/
130134
public function put($route, array $params = array(), array $headers = array())
131135
{
@@ -139,6 +143,7 @@ public function put($route, array $params = array(), array $headers = array())
139143
* @param array $headers
140144
* @return mixed
141145
* @throws HttpException
146+
* @throws CurlException
142147
*/
143148
public function delete($route, array $params = array(), array $headers = array())
144149
{
@@ -152,6 +157,7 @@ public function delete($route, array $params = array(), array $headers = array()
152157
* @param array $headers
153158
* @return mixed
154159
* @throws HttpException
160+
* @throws CurlException
155161
*/
156162
protected function call($http_method, $route, array $params = array(), array $headers = array())
157163
{
@@ -169,7 +175,7 @@ protected function call($http_method, $route, array $params = array(), array $he
169175
->setOption(CURLOPT_FOLLOWLOCATION, true)
170176
->setOption(CURLOPT_ENCODING, '');
171177

172-
foreach($this->curl_options as $option => $value) {
178+
foreach ($this->curl_options as $option => $value) {
173179
$this->request->setOption($option, $value);
174180
}
175181

@@ -203,7 +209,13 @@ protected function call($http_method, $route, array $params = array(), array $he
203209

204210
$this->request->setOption(CURLOPT_HTTPHEADER, $headers);
205211

212+
206213
$result = $this->request->execute();
214+
215+
if ($this->request->getErrorNumber()) {
216+
throw new CurlException($this->request->getErrorMessage(), $this->request->getErrorNumber());
217+
}
218+
207219
$last_response = $this->request->getInfo(CURLINFO_HTTP_CODE);
208220

209221
$this->request->close();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Kobas\Exception;
4+
5+
6+
/**
7+
* Class CurlException
8+
* @package Kobas\Exception
9+
*/
10+
class CurlException extends \Exception
11+
{
12+
13+
public function __construct($message, $code)
14+
{
15+
parent::__construct($message, $code);
16+
}
17+
}

src/Kobas/Request/Curl.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kobas\Request;
44

5+
56
/**
67
* Class Curl
78
* @package Kobas\Request
@@ -62,6 +63,20 @@ public function getInfo($name)
6263
return curl_getinfo($this->handle, $name);
6364
}
6465

66+
/**
67+
* @return int|mixed
68+
*/
69+
public function getErrorNumber() {
70+
return curl_errno($this->handle);
71+
}
72+
73+
/**
74+
* @return mixed|string
75+
*/
76+
public function getErrorMessage() {
77+
return curl_error($this->handle);
78+
}
79+
6580
/**
6681
* @return $this
6782
*/

src/Kobas/Request/HttpRequest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Kobas\Request;
44

5+
/**
6+
* Interface HttpRequest
7+
* @package Kobas\Request
8+
*/
59
interface HttpRequest
610
{
711
/**
@@ -27,8 +31,24 @@ public function setOption($name, $value);
2731
*/
2832
public function execute();
2933

34+
/**
35+
* @param $name
36+
* @return mixed
37+
*/
3038
public function getInfo($name);
3139

40+
41+
/**
42+
* @return mixed
43+
*/
44+
public function getErrorNumber();
45+
46+
/**
47+
* @return mixed
48+
*/
49+
public function getErrorMessage();
50+
51+
3252
/**
3353
* @return $this
3454
*/

0 commit comments

Comments
 (0)