Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 946a126

Browse files
committed
Do not convert TooManyRedirectsException
1 parent acbfce1 commit 946a126

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/Guzzle6HttpAdapter.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* For the full copyright and license information, please read the LICENSE
99
* file that was distributed with this source code.
1010
*/
11-
1211
namespace Http\Adapter;
1312

1413
use GuzzleHttp\Client;
@@ -46,37 +45,46 @@ public function sendRequest(RequestInterface $request)
4645
{
4746
try {
4847
return $this->client->send($request);
49-
} catch (Guzzle\RequestException $e) {
50-
throw $this->handleRequestException($e);
51-
} catch (Guzzle\GuzzleException $e) {
52-
// Assert: $e instance of SeekException or TransferException
48+
} catch (Guzzle\SeekException $e) {
5349
throw new RequestException($e->getMessage(), $request, $e);
50+
} catch (Guzzle\TransferException $e) {
51+
throw $this->handleTransferException($e);
5452
}
5553
}
5654

5755
/**
5856
* Converts a Guzzle exception into an Httplug exception.
5957
*
60-
* @param Guzzle\RequestException $exception
58+
* @param Guzzle\TransferException $exception
6159
*
6260
* @return RequestException
6361
*/
64-
private function handleRequestException(Guzzle\RequestException $exception)
62+
private function handleTransferException(Guzzle\TransferException $exception)
6563
{
66-
if ($exception instanceof Guzzle\ConnectException || $exception instanceof Guzzle\TooManyRedirectsException) {
64+
if ($exception instanceof Guzzle\ConnectException) {
6765
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
6866
}
6967

70-
// Make sure we have a response for the HttpException
71-
if ($exception->hasResponse()) {
72-
return new HttpException(
73-
$exception->getMessage(),
74-
$exception->getRequest(),
75-
$exception->getResponse(),
76-
$exception
77-
);
68+
if ($exception instanceof Guzzle\RequestException) {
69+
// Make sure we have a response for the HttpException
70+
if ($exception->hasResponse()) {
71+
return new HttpException(
72+
$exception->getMessage(),
73+
$exception->getRequest(),
74+
$exception->getResponse(),
75+
$exception
76+
);
77+
}
78+
79+
if ($exception instanceof Guzzle\TooManyRedirectsException) {
80+
// Do not convert TooManyRedirectsException
81+
return $exception;
82+
//return new HttpException($exception->getMessage(), $exception->getRequest(), null, $exception);
83+
}
84+
85+
return new RequestException($exception->getMessage(), $exception->getRequest(), $exception);
7886
}
7987

80-
return new RequestException($exception->getMessage(), $exception->getRequest(), $exception);
88+
return new TransferException($exception->getMessage(), 0, $exception);
8189
}
8290
}

tests/Guzzle6HttpAdapterExceptionTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function testGetException()
2525
$response = $this->getMock('Psr\Http\Message\ResponseInterface');
2626

2727
$adapter = new Guzzle6HttpAdapter();
28-
$method = new \ReflectionMethod('Http\Adapter\Guzzle6HttpAdapter', 'handleRequestException');
28+
$method = new \ReflectionMethod('Http\Adapter\Guzzle6HttpAdapter', 'handleTransferException');
2929
$method->setAccessible(true);
3030

3131
$outputException = $method->invoke($adapter, new Guzzle\ConnectException('foo', $request));
3232
$this->assertInstanceOf('Http\Client\Exception\NetworkException', $outputException, "Guzzle's ConnectException should be converted to a NetworkException");
3333

3434
$outputException = $method->invoke($adapter, new Guzzle\TooManyRedirectsException('foo', $request));
35-
$this->assertInstanceOf('Http\Client\Exception\NetworkException', $outputException, "Guzzle's TooManyRedirectsException should be converted to a NetworkException");
35+
$this->assertInstanceOf('GuzzleHttp\Exception\TooManyRedirectsException', $outputException, "Guzzle's TooManyRedirectsException should not be converted");
3636

3737
$outputException = $method->invoke($adapter, new Guzzle\RequestException('foo', $request, $response));
3838
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's RequestException should be converted to a HttpException");
@@ -46,6 +46,9 @@ public function testGetException()
4646
$outputException = $method->invoke($adapter, new Guzzle\ServerException('foo', $request, $response));
4747
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ServerException should be converted to a HttpException");
4848

49+
$outputException = $method->invoke($adapter, new Guzzle\TransferException('foo'));
50+
$this->assertInstanceOf('Http\Client\Exception\TransferException', $outputException, "Guzzle's TransferException should be converted to a TransferException");
51+
4952
/*
5053
* Test RequestException without response
5154
*/

0 commit comments

Comments
 (0)