Skip to content

Commit

Permalink
wrap Guzzle client exception
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Aug 20, 2017
1 parent a585948 commit 89ad361
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Exception/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@

class ErrorException extends \RuntimeException
{
/**
* @param \Exception $e
*
* @return ErrorException
*/
public static function wrap(\Exception $e)
{
return new self($e->getMessage(), $e->getCode(), $e);
}
}
6 changes: 5 additions & 1 deletion src/Service/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public function get($path, array $options = [])
);
}

$response = $this->client->request('GET', $this->host.$path, $options);
try {
$response = $this->client->request('GET', $this->host.$path, $options);
} catch (\Exception $e) {
throw $this->detector->wrap($e);
}

return $this->detector->detect($response);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Service/ErrorDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,30 @@
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Service;

use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\BannedException;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\ErrorException;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\NotFoundException;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;

class ErrorDetector
{
/**
* @param \Exception $exception
*
* @return ErrorException|NotFoundException
*/
public function wrap(\Exception $exception)
{
if ($exception instanceof ClientException &&
$exception->getResponse() instanceof ResponseInterface &&
$exception->getResponse()->getStatusCode() == 404
) {
return NotFoundException::page();
}

return ErrorException::wrap($exception);
}

/**
* @param ResponseInterface $response
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Service/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Tests\Service;

use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\ErrorException;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\Browser;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\ErrorDetector;
use GuzzleHttp\Client as HttpClient;
Expand Down Expand Up @@ -105,4 +106,28 @@ public function testGet($app_client)

$this->assertEquals($content, $this->browser->get($path, $params));
}

/**
* @expectedException \AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\ErrorException
*/
public function testException()
{
$exception = new \Exception();
$wrap = ErrorException::wrap($exception);

$this->detector
->expects($this->once())
->method('wrap')
->with($exception)
->will($this->returnValue($wrap))
;

$this->client
->expects($this->once())
->method('request')
->will($this->throwException($exception))
;

$this->browser->get('');
}
}
70 changes: 70 additions & 0 deletions tests/Service/ErrorDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Tests\Service;

use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\ErrorException;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\NotFoundException;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\ErrorDetector;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

Expand Down Expand Up @@ -102,4 +105,71 @@ public function testNoErrors()

$this->assertEquals($content, $this->detector->detect($this->response));
}

public function testWrapException()
{
$this->assertInstanceOf(ErrorException::class, $this->detector->wrap(new \Exception()));
}

public function testWrapNoResponseClientException()
{
/* @var $exception \PHPUnit_Framework_MockObject_MockObject|ClientException */
$exception = $this
->getMockBuilder(ClientException::class)
->disableOriginalConstructor()
->getMock()
;

$this->assertInstanceOf(ErrorException::class, $this->detector->wrap($exception));
}

public function testWrapClientException()
{
/* @var $response \PHPUnit_Framework_MockObject_MockObject|ResponseInterface */
$response = $this->getMock(ResponseInterface::class);
$response
->expects($this->once())
->method('getStatusCode')
->will($this->returnValue(401))
;

/* @var $exception \PHPUnit_Framework_MockObject_MockObject|ClientException */
$exception = $this
->getMockBuilder(ClientException::class)
->disableOriginalConstructor()
->getMock()
;
$exception
->expects($this->atLeastOnce())
->method('getResponse')
->will($this->returnValue($response))
;

$this->assertInstanceOf(ErrorException::class, $this->detector->wrap($exception));
}

public function testWrapNotFoundException()
{
/* @var $response \PHPUnit_Framework_MockObject_MockObject|ResponseInterface */
$response = $this->getMock(ResponseInterface::class);
$response
->expects($this->once())
->method('getStatusCode')
->will($this->returnValue(404))
;

/* @var $exception \PHPUnit_Framework_MockObject_MockObject|ClientException */
$exception = $this
->getMockBuilder(ClientException::class)
->disableOriginalConstructor()
->getMock()
;
$exception
->expects($this->atLeastOnce())
->method('getResponse')
->will($this->returnValue($response))
;

$this->assertInstanceOf(NotFoundException::class, $this->detector->wrap($exception));
}
}

0 comments on commit 89ad361

Please sign in to comment.