Skip to content

Commit 6f1f0b7

Browse files
committed
Adding an special exception in case the response returned by Guzzle is null, and fixed the method call to createResponse() to not create an fatal error.
1 parent 24f36e3 commit 6f1f0b7

File tree

3 files changed

+175
-10
lines changed

3 files changed

+175
-10
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* This file is part of the Tmdb PHP API created by Michael Roterman.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @package Tmdb
9+
* @author Michael Roterman <[email protected]>
10+
* @copyright (c) 2013, Michael Roterman
11+
* @version 0.0.1
12+
*/
13+
namespace Tmdb\Exception;
14+
use Tmdb\HttpClient\Request;
15+
16+
/**
17+
* Class RuntimeException
18+
* @package Tmdb\Exception
19+
*/
20+
class NullResponseException extends RuntimeException
21+
{
22+
/**
23+
* @var Request
24+
*/
25+
protected $request;
26+
27+
/**
28+
* @param Request $request
29+
* @param \Exception|null $previous
30+
*/
31+
public function __construct(Request $request, \Exception $previous = null)
32+
{
33+
$this->request = $request;
34+
35+
if (null !== $previous && is_a($previous, 'GuzzleHttp\Exception\RequestException')) {
36+
$message = sprintf(
37+
'The request to path "%s" with query parameters "%s" failed to return a valid response. The previous exception reported "%s" at "%s:%d".',
38+
$request->getPath(),
39+
json_encode($request->getParameters()->all()),
40+
$previous->getMessage(),
41+
$previous->getFile(),
42+
$previous->getLine()
43+
);
44+
} else {
45+
$message = sprintf(
46+
'The request to path "%s" with query parameters "%s" failed to return a valid response.',
47+
$request->getPath(),
48+
json_encode($request->getParameters()->all())
49+
);
50+
}
51+
52+
parent::__construct($message, 0, $previous);
53+
}
54+
55+
/**
56+
* @return Request
57+
*/
58+
public function getRequest()
59+
{
60+
return $this->request;
61+
}
62+
63+
/**
64+
* @param Request $request
65+
* @return $this
66+
*/
67+
public function setRequest(Request $request)
68+
{
69+
$this->request = $request;
70+
71+
return $this;
72+
}
73+
}

lib/Tmdb/HttpClient/Adapter/GuzzleAdapter.php

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
use GuzzleHttp\Client;
1616
use GuzzleHttp\ClientInterface;
1717
use GuzzleHttp\Exception\RequestException;
18-
use GuzzleHttp\Message\Response;
18+
use GuzzleHttp\Message\ResponseInterface;
1919
use GuzzleHttp\Subscriber\Retry\RetrySubscriber;
2020
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2121
use Tmdb\Common\ParameterBag;
22+
use Tmdb\Exception\NullResponseException;
2223
use Tmdb\HttpClient\Request;
24+
use Tmdb\HttpClient\Response;
2325

2426
class GuzzleAdapter extends AbstractAdapter
2527
{
@@ -28,6 +30,11 @@ class GuzzleAdapter extends AbstractAdapter
2830
*/
2931
private $client;
3032

33+
/**
34+
* @var Request
35+
*/
36+
protected $request;
37+
3138
public function __construct(ClientInterface $client = null, array $options = [])
3239
{
3340
if (null === $client) {
@@ -63,6 +70,8 @@ public function registerSubscribers(EventDispatcherInterface $eventDispatcher)
6370
*/
6471
public function getConfiguration(Request $request)
6572
{
73+
$this->request = $request;
74+
6675
return [
6776
'headers' => $request->getHeaders()->all(),
6877
'query' => $request->getParameters()->all()
@@ -72,12 +81,12 @@ public function getConfiguration(Request $request)
7281
/**
7382
* Create the response object
7483
*
75-
* @param Response $adapterResponse
84+
* @param ResponseInterface $adapterResponse
7685
* @return \Tmdb\HttpClient\Response
7786
*/
78-
private function createResponse(Response $adapterResponse)
87+
private function createResponse(ResponseInterface $adapterResponse = null)
7988
{
80-
$response = new \Tmdb\HttpClient\Response();
89+
$response = new Response();
8190

8291
$response->setCode($adapterResponse->getStatusCode());
8392
$response->setHeaders(new ParameterBag($adapterResponse->getHeaders()));
@@ -86,18 +95,40 @@ private function createResponse(Response $adapterResponse)
8695
return $response;
8796
}
8897

98+
/**
99+
* Create the request exception
100+
*
101+
* @param Request $request
102+
* @param RequestException|null $previousException
103+
* @throws \Tmdb\Exception\TmdbApiException
104+
*/
105+
protected function handleRequestException(Request $request, RequestException $previousException = null)
106+
{
107+
if (null !== $previousException && null == $response = $previousException->getResponse()) {
108+
throw new NullResponseException($this->request, $previousException);
109+
}
110+
111+
throw $this->createApiException(
112+
$request,
113+
$this->createResponse($previousException->getResponse()),
114+
$previousException
115+
);
116+
}
117+
89118
/**
90119
* {@inheritDoc}
91120
*/
92121
public function get(Request $request)
93122
{
123+
$response = null;
124+
94125
try {
95126
$response = $this->client->get(
96127
$request->getPath(),
97128
$this->getConfiguration($request)
98129
);
99130
} catch (RequestException $e) {
100-
throw $this->createApiException($request, $this->createResponse($e->getResponse()));
131+
$this->handleRequestException($request, $e);
101132
}
102133

103134
return $this->createResponse($response);
@@ -108,6 +139,8 @@ public function get(Request $request)
108139
*/
109140
public function post(Request $request)
110141
{
142+
$response = null;
143+
111144
try {
112145
$response = $this->client->post(
113146
$request->getPath(),
@@ -117,7 +150,7 @@ public function post(Request $request)
117150
)
118151
);
119152
} catch (RequestException $e) {
120-
throw $this->createApiException($request, $this->createResponse($e->getResponse()));
153+
$this->handleRequestException($request, $e);
121154
}
122155

123156
return $this->createResponse($response);
@@ -128,6 +161,8 @@ public function post(Request $request)
128161
*/
129162
public function put(Request $request)
130163
{
164+
$response = null;
165+
131166
try {
132167
$response = $this->client->put(
133168
$request->getPath(),
@@ -137,7 +172,7 @@ public function put(Request $request)
137172
)
138173
);
139174
} catch (RequestException $e) {
140-
throw $this->createApiException($request, $this->createResponse($e->getResponse()));
175+
$this->handleRequestException($request, $e);
141176
}
142177

143178
return $this->createResponse($response);
@@ -148,6 +183,8 @@ public function put(Request $request)
148183
*/
149184
public function patch(Request $request)
150185
{
186+
$response = null;
187+
151188
try {
152189
$response = $this->client->patch(
153190
$request->getPath(),
@@ -157,7 +194,7 @@ public function patch(Request $request)
157194
)
158195
);
159196
} catch (RequestException $e) {
160-
throw $this->createApiException($request, $this->createResponse($e->getResponse()));
197+
$this->handleRequestException($request, $e);
161198
}
162199

163200
return $this->createResponse($response);
@@ -168,6 +205,8 @@ public function patch(Request $request)
168205
*/
169206
public function delete(Request $request)
170207
{
208+
$response = null;
209+
171210
try {
172211
$response = $this->client->delete(
173212
$request->getPath(),
@@ -177,7 +216,7 @@ public function delete(Request $request)
177216
)
178217
);
179218
} catch (RequestException $e) {
180-
throw $this->createApiException($request, $this->createResponse($e->getResponse()));
219+
$this->handleRequestException($request, $e);
181220
}
182221

183222
return $this->createResponse($response);
@@ -188,13 +227,15 @@ public function delete(Request $request)
188227
*/
189228
public function head(Request $request)
190229
{
230+
$response = null;
231+
191232
try {
192233
$response = $this->client->head(
193234
$request->getPath(),
194235
$this->getConfiguration($request)
195236
);
196237
} catch (RequestException $e) {
197-
throw $this->createApiException($request, $this->createResponse($e->getResponse()));
238+
$this->handleRequestException($request, $e);
198239
}
199240

200241
return $this->createResponse($response);

test/Tmdb/Tests/HttpClient/Adapter/GuzzleAdapterTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use GuzzleHttp\Exception\RequestException;
1717
use GuzzleHttp\Message\Response;
1818
use GuzzleHttp\Stream\Stream;
19+
use Tmdb\Exception\NullResponseException;
1920
use Tmdb\HttpClient\Adapter\GuzzleAdapter;
2021
use Tmdb\HttpClient\Request;
2122
use Tmdb\Tests\TestCase;
@@ -274,6 +275,56 @@ public function shouldThrowExceptionForDelete()
274275
$adapter->delete(new Request());
275276
}
276277

278+
/**
279+
* @expectedException \Tmdb\Exception\NullResponseException
280+
* @test
281+
*/
282+
public function shouldThrowNullResponseException()
283+
{
284+
$client = $this->getMock('GuzzleHttp\ClientInterface');
285+
286+
$client->expects($this->once())
287+
->method('get')
288+
->will($this->throwException(
289+
new RequestException(
290+
'404',
291+
new \GuzzleHttp\Message\Request('get', '/'),
292+
null
293+
)
294+
))
295+
;
296+
297+
$adapter = new GuzzleAdapter($client);
298+
$adapter->get(new Request());
299+
}
300+
301+
/**
302+
* @test
303+
*/
304+
public function shouldFormatMessageForAnGuzzleHttpRequestException()
305+
{
306+
$client = $this->getMock('GuzzleHttp\ClientInterface');
307+
308+
$client->expects($this->once())
309+
->method('get')
310+
->will($this->throwException(
311+
new RequestException(
312+
'404',
313+
new \GuzzleHttp\Message\Request('get', '/'),
314+
null
315+
)
316+
))
317+
;
318+
319+
$adapter = new GuzzleAdapter($client);
320+
321+
try {
322+
$adapter->get(new Request());
323+
} catch (NullResponseException $e) {
324+
$this->assertEquals(true, false !== strpos($e->getMessage(), 'previous exception'));
325+
}
326+
}
327+
277328
/**
278329
* @test
279330
*/

0 commit comments

Comments
 (0)