From bd87166fe8d41ec7d9b80153554c222671385d13 Mon Sep 17 00:00:00 2001 From: David Grayston <1229335+davidgrayston@users.noreply.github.com> Date: Fri, 27 Dec 2019 10:03:14 +0000 Subject: [PATCH] PSR-18: Network / Request exception inheritance (#158) #155 Distinct Network and Request exceptions --- CHANGELOG.md | 4 ++++ spec/Exception/NetworkExceptionSpec.php | 24 +++++++++++++++++++++-- spec/Exception/RequestExceptionSpec.php | 16 ++++++++++++++- src/Exception/NetworkException.php | 16 ++++++++++++++- src/Exception/RequestAwareTrait.php | 26 +++++++++++++++++++++++++ src/Exception/RequestException.php | 12 ++---------- 6 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 src/Exception/RequestAwareTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a87403d..48b5c17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Changed + +- `Http\Client\Exception\NetworkException` no longer extends `Http\Client\Exception\RequestException`, + in accordance with [PSR-18](https://www.php-fig.org/psr/psr-18/) ## [2.0.0] - 2018-10-31 diff --git a/spec/Exception/NetworkExceptionSpec.php b/spec/Exception/NetworkExceptionSpec.php index b98708b..c628b82 100644 --- a/spec/Exception/NetworkExceptionSpec.php +++ b/spec/Exception/NetworkExceptionSpec.php @@ -17,8 +17,28 @@ function it_is_initializable() $this->shouldHaveType('Http\Client\Exception\NetworkException'); } - function it_is_a_request_exception() + function it_is_a_transfer_exception() { - $this->shouldHaveType('Http\Client\Exception\RequestException'); + $this->shouldHaveType('Http\Client\Exception\TransferException'); + } + + function it_implements_psr_network_exception_interface() + { + $this->shouldHaveType('Psr\Http\Client\NetworkExceptionInterface'); + } + + function it_does_not_implement_psr_request_exception_interface() + { + $this->shouldNotHaveType('Psr\Http\Client\RequestExceptionInterface'); + } + + function it_is_not_a_request_exception() + { + $this->shouldNotHaveType('Http\Client\Exception\RequestException'); + } + + function it_has_a_request(RequestInterface $request) + { + $this->getRequest()->shouldReturn($request); } } diff --git a/spec/Exception/RequestExceptionSpec.php b/spec/Exception/RequestExceptionSpec.php index 92dc182..5ac7a10 100644 --- a/spec/Exception/RequestExceptionSpec.php +++ b/spec/Exception/RequestExceptionSpec.php @@ -2,7 +2,6 @@ namespace spec\Http\Client\Exception; -use Http\Client\Exception\RequestException; use Psr\Http\Message\RequestInterface; use PhpSpec\ObjectBehavior; @@ -23,6 +22,21 @@ function it_is_a_transfer_exception() $this->shouldHaveType('Http\Client\Exception\TransferException'); } + function it_implements_psr_request_exception_interface() + { + $this->shouldHaveType('Psr\Http\Client\RequestExceptionInterface'); + } + + function it_does_not_implement_psr_network_exception_interface() + { + $this->shouldNotHaveType('Psr\Http\Client\NetworkExceptionInterface'); + } + + function it_is_not_a_network_exception() + { + $this->shouldNotHaveType('Http\Client\Exception\NetworkException'); + } + function it_has_a_request(RequestInterface $request) { $this->getRequest()->shouldReturn($request); diff --git a/src/Exception/NetworkException.php b/src/Exception/NetworkException.php index 62b912a..d52a356 100644 --- a/src/Exception/NetworkException.php +++ b/src/Exception/NetworkException.php @@ -2,6 +2,7 @@ namespace Http\Client\Exception; +use Psr\Http\Message\RequestInterface; use Psr\Http\Client\NetworkExceptionInterface as PsrNetworkException; /** @@ -11,6 +12,19 @@ * * @author Márk Sági-Kazár */ -class NetworkException extends RequestException implements PsrNetworkException +class NetworkException extends TransferException implements PsrNetworkException { + use RequestAwareTrait; + + /** + * @param string $message + * @param RequestInterface $request + * @param \Exception|null $previous + */ + public function __construct($message, RequestInterface $request, \Exception $previous = null) + { + $this->setRequest($request); + + parent::__construct($message, 0, $previous); + } } diff --git a/src/Exception/RequestAwareTrait.php b/src/Exception/RequestAwareTrait.php new file mode 100644 index 0000000..71b4bb8 --- /dev/null +++ b/src/Exception/RequestAwareTrait.php @@ -0,0 +1,26 @@ +request = $request; + } + + /** + * {@inheritdoc} + */ + public function getRequest(): RequestInterface + { + return $this->request; + } +} diff --git a/src/Exception/RequestException.php b/src/Exception/RequestException.php index 1f0176c..b548335 100644 --- a/src/Exception/RequestException.php +++ b/src/Exception/RequestException.php @@ -15,10 +15,7 @@ */ class RequestException extends TransferException implements PsrRequestException { - /** - * @var RequestInterface - */ - private $request; + use RequestAwareTrait; /** * @param string $message @@ -27,13 +24,8 @@ class RequestException extends TransferException implements PsrRequestException */ public function __construct($message, RequestInterface $request, \Exception $previous = null) { - $this->request = $request; + $this->setRequest($request); parent::__construct($message, 0, $previous); } - - public function getRequest(): RequestInterface - { - return $this->request; - } }