Skip to content

Commit 4e2c62a

Browse files
committed
improved error reporting
1 parent 825965c commit 4e2c62a

File tree

5 files changed

+65
-34
lines changed

5 files changed

+65
-34
lines changed

src/Embed.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ public static function create($request, array $config = array())
4545
return $info;
4646
}
4747

48-
if (!$request->isValid()) {
49-
throw new Exceptions\InvalidUrlException("The url '{$request->getUrl()}' returns the http code '{$request->getHttpCode()}'");
48+
$error = $request->getError();
49+
50+
if (empty($error)) {
51+
throw new Exceptions\InvalidUrlException(sprintf("The url '%s' returns the http code %s", $request->getUrl(), $request->getHttpCode()));
5052
}
5153

52-
throw new Exceptions\InvalidUrlException("The url '{$request->getUrl()}' is not supported");
54+
throw new Exceptions\InvalidUrlException(sprintf("The url '%s' returns the following error: %s", $request->getUrl(), $error));
5355
}
5456

5557
/**

src/Request.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ public function getMimeType()
172172
return $this->getResolver()->getMimeType();
173173
}
174174

175+
/**
176+
* Get the connection error.
177+
*
178+
* @return string|null
179+
*/
180+
public function getError()
181+
{
182+
return $this->getResolver()->getError();
183+
}
184+
175185
/**
176186
* Get the content of the url.
177187
*

src/RequestResolvers/Curl.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Curl implements RequestResolverInterface
1818
CURLOPT_MAXREDIRS => 20,
1919
CURLOPT_CONNECTTIMEOUT => 10,
2020
CURLOPT_TIMEOUT => 10,
21+
CURLOPT_SSL_VERIFYHOST => false,
2122
CURLOPT_SSL_VERIFYPEER => false,
2223
CURLOPT_ENCODING => '',
2324
CURLOPT_AUTOREFERER => true,
@@ -38,14 +39,6 @@ public function __construct($url, array $config)
3839
{
3940
$this->url = $url;
4041

41-
var_dump(ini_get('open_basedir'));
42-
var_dump(ini_get('safe_mode'));
43-
44-
//http://stackoverflow.com/questions/14054652/getting-301-with-curl-despite-followlocation
45-
if (ini_get('open_basedir') !== '' && ini_get('safe_mode') === false) {
46-
$this->config[CURLOPT_SSL_VERIFYHOST] = false;
47-
}
48-
4942
$this->config = $config + $this->config;
5043
}
5144

@@ -73,6 +66,14 @@ public function getMimeType()
7366
return $this->getResult('mime_type');
7467
}
7568

69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function getError()
73+
{
74+
return $this->getResult('error');
75+
}
76+
7677
/**
7778
* {@inheritdoc}
7879
*/

src/RequestResolvers/Guzzle5.php

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Guzzle5 implements RequestResolverInterface
2121
*/
2222
protected $response;
2323

24+
protected $error;
25+
2426
protected $defaultConfig = [
2527
'verify' => false,
2628
'timeout' => 10,
@@ -50,64 +52,73 @@ public function __construct($url, array $config)
5052
}
5153

5254
/**
53-
* Get the http code of the url, for example: 200.
54-
*
55-
* @return int The http code
55+
* {@inheritdoc}
5656
*/
5757
public function getHttpCode()
5858
{
59-
return $this->getResponse()->getStatusCode();
59+
$response = $this->getResponse();
60+
61+
return $response ? $response->getStatusCode() : null;
6062
}
6163

6264
/**
63-
* Get the content-type of the url, for example: text/html.
64-
*
65-
* @return string The content-type header or null
65+
* {@inheritdoc}
6666
*/
6767
public function getMimeType()
6868
{
69-
return $this->getResponse()->getHeader('Content-Type');
69+
$response = $this->getResponse();
70+
71+
return $response ? $response->getHeader('Content-Type') : null;
7072
}
7173

7274
/**
73-
* Get the content of the url.
74-
*
75-
* @return string The content or false
75+
* {@inheritdoc}
76+
*/
77+
public function getError()
78+
{
79+
return $error;
80+
}
81+
82+
/**
83+
* {@inheritdoc}
7684
*/
7785
public function getContent()
7886
{
79-
return $this->getResponse()->getBody()->getContents();
87+
$response = $this->getResponse();
88+
89+
return $response ? $response->getBody()->getContents() : null;
8090
}
8191

8292
/**
83-
* Return the final url (after all possible redirects).
84-
*
85-
* @return string The final url
93+
* {@inheritdoc}
8694
*/
8795
public function getUrl()
8896
{
89-
return $this->getResponse()->getEffectiveUrl();
97+
$response = $this->getResponse();
98+
99+
return $response ? $response->getEffectiveUrl() : null;
90100
}
91101

92102
/**
93-
* Return the http request info (for debug purposes).
94-
*
95-
* @return array
103+
* {@inheritdoc}
96104
*/
97105
public function getRequestInfo()
98106
{
99107
return $this->request->getConfig();
100108
}
101109

102110
/**
103-
* Get the result of the http request.
104-
*
105-
* @return Response
111+
* {@inheritdoc}
106112
*/
107113
protected function getResponse()
108114
{
109115
if ($this->response === null) {
110-
$this->response = $this->client->send($this->request);
116+
try {
117+
$this->response = $this->client->send($this->request);
118+
} catch (\Exception $exception) {
119+
$error = $exception->getMessage();
120+
$this->response = false;
121+
}
111122
}
112123

113124
return $this->response;

src/RequestResolvers/RequestResolverInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function getHttpCode();
2929
*/
3030
public function getMimeType();
3131

32+
/**
33+
* Returns the connection error if exists
34+
*
35+
* @return string|null
36+
*/
37+
public function getError();
38+
3239
/**
3340
* Get the content of the url.
3441
*

0 commit comments

Comments
 (0)