Skip to content

PSR-18: Network / Request exception inheritance #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 27, 2019
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 22 additions & 2 deletions spec/Exception/NetworkExceptionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 15 additions & 1 deletion spec/Exception/RequestExceptionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace spec\Http\Client\Exception;

use Http\Client\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use PhpSpec\ObjectBehavior;

Expand All @@ -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);
Expand Down
16 changes: 15 additions & 1 deletion src/Exception/NetworkException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Http\Client\Exception;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Client\NetworkExceptionInterface as PsrNetworkException;

/**
Expand All @@ -11,6 +12,19 @@
*
* @author Márk Sági-Kazár <[email protected]>
*/
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);
}
}
26 changes: 26 additions & 0 deletions src/Exception/RequestAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Http\Client\Exception;

use Psr\Http\Message\RequestInterface;

trait RequestAwareTrait
{
/**
* @var RequestInterface
*/
private $request;

private function setRequest(RequestInterface $request)
{
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public function getRequest(): RequestInterface
{
return $this->request;
}
}
12 changes: 2 additions & 10 deletions src/Exception/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
class RequestException extends TransferException implements PsrRequestException
{
/**
* @var RequestInterface
*/
private $request;
use RequestAwareTrait;

/**
* @param string $message
Expand All @@ -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;
}
}