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

Fix for issue 8 #10

Merged
merged 1 commit into from
Nov 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": ">=5.5.0",
"php-http/httplug": "^1.0",
"php-http/httplug": "dev-master",
"guzzlehttp/guzzle": "^6.0"
},
"require-dev": {
Expand Down
37 changes: 27 additions & 10 deletions src/Guzzle6HttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Adapter;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception as GuzzleExceptions;
use Http\Client\Exception;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\NetworkException;
use Http\Client\Exception;
use Http\Client\Exception\RequestException;
use Http\Client\Exception\TransferException;
use Http\Client\HttpClient;
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -45,24 +46,40 @@ public function sendRequest(RequestInterface $request)
{
try {
return $this->client->send($request);
} catch (RequestException $e) {
throw $this->createException($e);
} catch (GuzzleExceptions\SeekException $e) {
throw new RequestException($e->getMessage(), $request, $e);
} catch (GuzzleExceptions\GuzzleException $e) {
throw $this->handleException($e);
}
}

/**
* Converts a Guzzle exception into an Httplug exception.
*
* @param RequestException $exception
* @param GuzzleExceptions\GuzzleException $exception
*
* @return Exception
*/
private function createException(RequestException $exception)
private function handleException(GuzzleExceptions\GuzzleException $exception)
{
if ($exception->hasResponse()) {
return new HttpException($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception);
if ($exception instanceof GuzzleExceptions\ConnectException) {
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
}

if ($exception instanceof GuzzleExceptions\RequestException) {
// Make sure we have a response for the HttpException
if ($exception->hasResponse()) {
return new HttpException(
$exception->getMessage(),
$exception->getRequest(),
$exception->getResponse(),
$exception
);
}

return new RequestException($exception->getMessage(), $exception->getRequest(), $exception);
}

return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
return new TransferException($exception->getMessage(), 0, $exception);
}
}
67 changes: 67 additions & 0 deletions tests/Guzzle6HttpAdapterExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Http Adapter package.
*
* (c) Eric GELOEN <[email protected]>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Adapter\Tests;

use GuzzleHttp\Exception as GuzzleExceptions;
use Http\Adapter\Guzzle6HttpAdapter;

/**
* @author Tobias Nyholm <[email protected]>
*/
class Guzzle6HttpAdapterExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testGetException()
{
$request = $this->getMock('Psr\Http\Message\RequestInterface');
$response = $this->getMock('Psr\Http\Message\ResponseInterface');

$adapter = new Guzzle6HttpAdapter();
$method = new \ReflectionMethod('Http\Adapter\Guzzle6HttpAdapter', 'handleException');
$method->setAccessible(true);

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

$outputException = $method->invoke($adapter, new GuzzleExceptions\TooManyRedirectsException('foo', $request));
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's TooManyRedirectsException should be converted to a RequestException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request, $response));
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's RequestException should be converted to a HttpException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request, $response));
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's BadResponseException should be converted to a HttpException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request, $response));
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ClientException should be converted to a HttpException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request, $response));
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ServerException should be converted to a HttpException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\TransferException('foo'));
$this->assertInstanceOf('Http\Client\Exception\TransferException', $outputException, "Guzzle's TransferException should be converted to a TransferException");

/*
* Test RequestException without response
*/
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request));
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's RequestException with no response should be converted to a RequestException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request));
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's BadResponseException with no response should be converted to a RequestException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request));
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ClientException with no response should be converted to a RequestException");

$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request));
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ServerException with no response should be converted to a RequestException");
}
}