Skip to content

Commit bd87166

Browse files
davidgraystondbu
authored andcommitted
PSR-18: Network / Request exception inheritance (#158)
#155 Distinct Network and Request exceptions
1 parent ec4c56a commit bd87166

File tree

6 files changed

+84
-14
lines changed

6 files changed

+84
-14
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
## [Unreleased]
1111

12+
### Changed
13+
14+
- `Http\Client\Exception\NetworkException` no longer extends `Http\Client\Exception\RequestException`,
15+
in accordance with [PSR-18](https://www.php-fig.org/psr/psr-18/)
1216

1317
## [2.0.0] - 2018-10-31
1418

spec/Exception/NetworkExceptionSpec.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,28 @@ function it_is_initializable()
1717
$this->shouldHaveType('Http\Client\Exception\NetworkException');
1818
}
1919

20-
function it_is_a_request_exception()
20+
function it_is_a_transfer_exception()
2121
{
22-
$this->shouldHaveType('Http\Client\Exception\RequestException');
22+
$this->shouldHaveType('Http\Client\Exception\TransferException');
23+
}
24+
25+
function it_implements_psr_network_exception_interface()
26+
{
27+
$this->shouldHaveType('Psr\Http\Client\NetworkExceptionInterface');
28+
}
29+
30+
function it_does_not_implement_psr_request_exception_interface()
31+
{
32+
$this->shouldNotHaveType('Psr\Http\Client\RequestExceptionInterface');
33+
}
34+
35+
function it_is_not_a_request_exception()
36+
{
37+
$this->shouldNotHaveType('Http\Client\Exception\RequestException');
38+
}
39+
40+
function it_has_a_request(RequestInterface $request)
41+
{
42+
$this->getRequest()->shouldReturn($request);
2343
}
2444
}

spec/Exception/RequestExceptionSpec.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace spec\Http\Client\Exception;
44

5-
use Http\Client\Exception\RequestException;
65
use Psr\Http\Message\RequestInterface;
76
use PhpSpec\ObjectBehavior;
87

@@ -23,6 +22,21 @@ function it_is_a_transfer_exception()
2322
$this->shouldHaveType('Http\Client\Exception\TransferException');
2423
}
2524

25+
function it_implements_psr_request_exception_interface()
26+
{
27+
$this->shouldHaveType('Psr\Http\Client\RequestExceptionInterface');
28+
}
29+
30+
function it_does_not_implement_psr_network_exception_interface()
31+
{
32+
$this->shouldNotHaveType('Psr\Http\Client\NetworkExceptionInterface');
33+
}
34+
35+
function it_is_not_a_network_exception()
36+
{
37+
$this->shouldNotHaveType('Http\Client\Exception\NetworkException');
38+
}
39+
2640
function it_has_a_request(RequestInterface $request)
2741
{
2842
$this->getRequest()->shouldReturn($request);

src/Exception/NetworkException.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Client\Exception;
44

5+
use Psr\Http\Message\RequestInterface;
56
use Psr\Http\Client\NetworkExceptionInterface as PsrNetworkException;
67

78
/**
@@ -11,6 +12,19 @@
1112
*
1213
* @author Márk Sági-Kazár <[email protected]>
1314
*/
14-
class NetworkException extends RequestException implements PsrNetworkException
15+
class NetworkException extends TransferException implements PsrNetworkException
1516
{
17+
use RequestAwareTrait;
18+
19+
/**
20+
* @param string $message
21+
* @param RequestInterface $request
22+
* @param \Exception|null $previous
23+
*/
24+
public function __construct($message, RequestInterface $request, \Exception $previous = null)
25+
{
26+
$this->setRequest($request);
27+
28+
parent::__construct($message, 0, $previous);
29+
}
1630
}

src/Exception/RequestAwareTrait.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
trait RequestAwareTrait
8+
{
9+
/**
10+
* @var RequestInterface
11+
*/
12+
private $request;
13+
14+
private function setRequest(RequestInterface $request)
15+
{
16+
$this->request = $request;
17+
}
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function getRequest(): RequestInterface
23+
{
24+
return $this->request;
25+
}
26+
}

src/Exception/RequestException.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
*/
1616
class RequestException extends TransferException implements PsrRequestException
1717
{
18-
/**
19-
* @var RequestInterface
20-
*/
21-
private $request;
18+
use RequestAwareTrait;
2219

2320
/**
2421
* @param string $message
@@ -27,13 +24,8 @@ class RequestException extends TransferException implements PsrRequestException
2724
*/
2825
public function __construct($message, RequestInterface $request, \Exception $previous = null)
2926
{
30-
$this->request = $request;
27+
$this->setRequest($request);
3128

3229
parent::__construct($message, 0, $previous);
3330
}
34-
35-
public function getRequest(): RequestInterface
36-
{
37-
return $this->request;
38-
}
3931
}

0 commit comments

Comments
 (0)