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

Commit ba12173

Browse files
committed
Fix for issue #8
1 parent baccb2b commit ba12173

File tree

2 files changed

+94
-10
lines changed

2 files changed

+94
-10
lines changed

src/Guzzle6HttpAdapter.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
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;
1514
use GuzzleHttp\ClientInterface;
16-
use GuzzleHttp\Exception\RequestException;
15+
use GuzzleHttp\Exception as GuzzleExceptions;
16+
use Http\Client\Exception;
1717
use Http\Client\Exception\HttpException;
1818
use Http\Client\Exception\NetworkException;
19-
use Http\Client\Exception;
19+
use Http\Client\Exception\RequestException;
20+
use Http\Client\Exception\TransferException;
2021
use Http\Client\HttpClient;
2122
use Psr\Http\Message\RequestInterface;
2223

@@ -45,24 +46,40 @@ public function sendRequest(RequestInterface $request)
4546
{
4647
try {
4748
return $this->client->send($request);
48-
} catch (RequestException $e) {
49-
throw $this->createException($e);
49+
} catch (GuzzleExceptions\SeekException $e) {
50+
throw new RequestException($e->getMessage(), $request, $e);
51+
} catch (GuzzleExceptions\GuzzleException $e) {
52+
throw $this->handleException($e);
5053
}
5154
}
5255

5356
/**
5457
* Converts a Guzzle exception into an Httplug exception.
5558
*
56-
* @param RequestException $exception
59+
* @param GuzzleExceptions\GuzzleException $exception
5760
*
5861
* @return Exception
5962
*/
60-
private function createException(RequestException $exception)
63+
private function handleException(GuzzleExceptions\GuzzleException $exception)
6164
{
62-
if ($exception->hasResponse()) {
63-
return new HttpException($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception);
65+
if ($exception instanceof GuzzleExceptions\ConnectException) {
66+
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
67+
}
68+
69+
if ($exception instanceof GuzzleExceptions\RequestException) {
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+
);
78+
}
79+
80+
return new RequestException($exception->getMessage(), $exception->getRequest(), $exception);
6481
}
6582

66-
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
83+
return new TransferException($exception->getMessage(), 0, $exception);
6784
}
6885
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Http Adapter package.
5+
*
6+
* (c) Eric GELOEN <[email protected]>
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Http\Adapter\Tests;
13+
14+
use GuzzleHttp\Exception as GuzzleExceptions;
15+
use Http\Adapter\Guzzle6HttpAdapter;
16+
17+
/**
18+
* @author Tobias Nyholm <[email protected]>
19+
*/
20+
class Guzzle6HttpAdapterExceptionTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testGetException()
23+
{
24+
$request = $this->getMock('Psr\Http\Message\RequestInterface');
25+
$response = $this->getMock('Psr\Http\Message\ResponseInterface');
26+
27+
$adapter = new Guzzle6HttpAdapter();
28+
$method = new \ReflectionMethod('Http\Adapter\Guzzle6HttpAdapter', 'handleException');
29+
$method->setAccessible(true);
30+
31+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ConnectException('foo', $request));
32+
$this->assertInstanceOf('Http\Client\Exception\NetworkException', $outputException, "Guzzle's ConnectException should be converted to a NetworkException");
33+
34+
$outputException = $method->invoke($adapter, new GuzzleExceptions\TooManyRedirectsException('foo', $request));
35+
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's TooManyRedirectsException should be converted to a RequestException");
36+
37+
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request, $response));
38+
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's RequestException should be converted to a HttpException");
39+
40+
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request, $response));
41+
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's BadResponseException should be converted to a HttpException");
42+
43+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request, $response));
44+
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ClientException should be converted to a HttpException");
45+
46+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request, $response));
47+
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ServerException should be converted to a HttpException");
48+
49+
$outputException = $method->invoke($adapter, new GuzzleExceptions\TransferException('foo'));
50+
$this->assertInstanceOf('Http\Client\Exception\TransferException', $outputException, "Guzzle's TransferException should be converted to a TransferException");
51+
52+
/*
53+
* Test RequestException without response
54+
*/
55+
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request));
56+
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's RequestException with no response should be converted to a RequestException");
57+
58+
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request));
59+
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's BadResponseException with no response should be converted to a RequestException");
60+
61+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request));
62+
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ClientException with no response should be converted to a RequestException");
63+
64+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request));
65+
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ServerException with no response should be converted to a RequestException");
66+
}
67+
}

0 commit comments

Comments
 (0)