Skip to content

Commit 8063300

Browse files
committed
improved error info in demo
1 parent 86c3b2a commit 8063300

File tree

8 files changed

+88
-18
lines changed

8 files changed

+88
-18
lines changed

demo/index.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,28 @@ function printCode($code, $asHtml = true)
224224
<section>
225225
<h1>Result:</h1>
226226

227-
<?php $info = Embed\Embed::create(getUrl(), $options); ?>
227+
<?php
228+
try {
229+
$dispatcher = new Embed\Http\CurlDispatcher();
230+
$info = Embed\Embed::create(getUrl(), $options, $dispatcher);
231+
} catch (Exception $exception) {
232+
echo '<table>';
233+
foreach ($dispatcher->getAllResponses() as $response) {
234+
echo '<tr>';
235+
echo '<th>'.$response->getUrl().'</th>';
236+
echo '</tr><tr><td>';
237+
printHeaders($response->getHeaders());
238+
echo '</td><tr><td><pre>';
239+
printArray($response->getInfo());
240+
echo '</td><tr><td><pre>';
241+
printText($response->getContent());
242+
echo '</pre></td></tr>';
243+
}
244+
echo '</table>';
245+
246+
throw $exception;
247+
}
248+
?>
228249

229250
<table>
230251
<?php foreach ($adapterData as $name => $fn): ?>

src/Embed.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ private static function process(Url $url, array $config, DispatcherInterface $di
7575
return new Adapters\Webpage($response, $config, $dispatcher);
7676
}
7777

78-
throw new Exceptions\InvalidUrlException(sprintf("Invalid url '%s' (%s)", (string) $url, $response->getStatusCode()));
78+
$exception = new Exceptions\InvalidUrlException(sprintf("Invalid url '%s' (Status code %s)", (string) $url, $response->getStatusCode()));
79+
80+
$exception->setResponse($response);
81+
82+
throw $exception;
83+
7984
}
8085
}

src/Exceptions/InvalidUrlException.php

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

33
namespace Embed\Exceptions;
44

5+
use Embed\Http\Response;
6+
57
class InvalidUrlException extends EmbedException
68
{
9+
/**
10+
* @var Response|null
11+
*/
12+
private $response;
13+
14+
/**
15+
* Set the response related with this error
16+
*
17+
* @param Response
18+
*/
19+
public function setResponse(Response $response)
20+
{
21+
$this->response = $response;
22+
}
23+
24+
/**
25+
* Get the response related with this error
26+
*
27+
* @return Response|null
28+
*/
29+
public function getResponse()
30+
{
31+
return $this->response;
32+
}
733
}

src/Http/AbstractResponse.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ abstract class AbstractResponse
1212
protected $statusCode;
1313
protected $contentType;
1414
protected $headers;
15+
protected $info;
1516

16-
public function __construct(Url $startingUrl, Url $url, $statusCode, $contentType, array $headers)
17+
public function __construct(Url $startingUrl, Url $url, $statusCode, $contentType, array $headers, array $info)
1718
{
1819
$this->startingUrl = $startingUrl;
1920
$this->url = $url;
2021
$this->statusCode = $statusCode;
2122
$this->contentType = $contentType;
2223
$this->headers = $headers;
24+
$this->info = $info;
2325
}
2426

2527
/**
@@ -72,6 +74,16 @@ public function getHeaders()
7274
return $this->headers;
7375
}
7476

77+
/**
78+
* Returns extra http info.
79+
*
80+
* @return array
81+
*/
82+
public function getInfo()
83+
{
84+
return $this->info;
85+
}
86+
7587
/**
7688
* Get a header.
7789
*

src/Http/CurlDispatcher.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ public function __construct(array $config = [])
3434
{
3535
$this->config = $config + $this->config;
3636

37-
$cookies = str_replace('//', '/', sys_get_temp_dir().'/embed-cookies.'.uniqid());
37+
if (!isset($this->config[CURLOPT_COOKIEJAR])) {
38+
$cookies = str_replace('//', '/', sys_get_temp_dir().'/embed-cookies.'.uniqid());
3839

39-
if (is_file($cookies)) {
40-
if (!is_writable($cookies)) {
41-
throw new EmbedException(sprintf('The temporary cookies file "%s" is not writable', $cookies));
40+
if (is_file($cookies)) {
41+
if (!is_writable($cookies)) {
42+
throw new EmbedException(sprintf('The temporary cookies file "%s" is not writable', $cookies));
43+
}
44+
} elseif (!is_writable(dirname($cookies))) {
45+
throw new EmbedException(sprintf('The temporary folder "%s" is not writable', dirname($cookies)));
4246
}
43-
} elseif (!is_writable(dirname($cookies))) {
44-
throw new EmbedException(sprintf('The temporary folder "%s" is not writable', dirname($cookies)));
45-
}
4647

47-
$this->config[CURLOPT_COOKIEJAR] = $cookies;
48-
$this->config[CURLOPT_COOKIEFILE] = $cookies;
48+
$this->config[CURLOPT_COOKIEJAR] = $cookies;
49+
$this->config[CURLOPT_COOKIEFILE] = $cookies;
50+
}
4951
}
5052

5153
/**
@@ -103,7 +105,8 @@ public function dispatch(Url $url)
103105
$result['statusCode'],
104106
$result['contentType'],
105107
$result['content'],
106-
$result['headers']
108+
$result['headers'],
109+
$result['info']
107110
);
108111
}
109112

@@ -196,7 +199,8 @@ public function dispatchImages(array $urls)
196199
$result['statusCode'],
197200
$result['contentType'],
198201
[$result['data']->width, $result['data']->height],
199-
$result['headers']
202+
$result['headers'],
203+
$result['info']
200204
);
201205
}
202206
}

src/Http/CurlResult.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getResult()
3939
'url' => isset($result['url']) ? $result['url'] : null,
4040
'statusCode' => isset($result['http_code']) ? $result['http_code'] : null,
4141
'contentType' => isset($result['content_type']) ? $result['content_type'] : null,
42+
'info' => $result,
4243
'content' => $this->body,
4344
'headers' => $this->headers,
4445
'data' => $this->data,

src/Http/ImageResponse.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ public static function createFromBase64(Url $url)
3131
200,
3232
$info['mime'],
3333
[$info[0], $info[1]],
34+
[],
3435
[]
3536
);
3637
}
3738
}
3839

39-
public function __construct(Url $startingUrl, Url $url, $statusCode, $contentType, $size, array $headers)
40+
public function __construct(Url $startingUrl, Url $url, $statusCode, $contentType, $size, array $headers, array $info)
4041
{
41-
parent::__construct($startingUrl, $url, $statusCode, $contentType, $headers);
42+
parent::__construct($startingUrl, $url, $statusCode, $contentType, $headers, $info);
4243
$this->size = $size;
4344
}
4445

src/Http/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Response extends AbstractResponse
1717
protected $jsonContent;
1818
protected $htmlContent;
1919

20-
public function __construct(Url $startingUrl, Url $url, $statusCode, $contentType, $content, array $headers)
20+
public function __construct(Url $startingUrl, Url $url, $statusCode, $contentType, $content, array $headers, array $info)
2121
{
22-
parent::__construct($startingUrl, $url, $statusCode, $contentType, $headers);
22+
parent::__construct($startingUrl, $url, $statusCode, $contentType, $headers, $info);
2323
$this->setContent($content);
2424
}
2525

0 commit comments

Comments
 (0)