Skip to content

Commit 67baf37

Browse files
committed
Merge pull request #43 from php-http/exceptions
Refactor exceptions
2 parents fb19763 + 58a3d52 commit 67baf37

18 files changed

+496
-309
lines changed

spec/Exception/BatchExceptionSpec.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Http\Client\Exception\TransferException;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class BatchExceptionSpec extends ObjectBehavior
10+
{
11+
function let(TransferException $e, ResponseInterface $response)
12+
{
13+
$this->beConstructedWith([$e], [$response]);
14+
}
15+
16+
function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Http\Client\Exception\BatchException');
19+
}
20+
21+
function it_is_a_transfer_exception()
22+
{
23+
$this->shouldHaveType('Http\Client\Exception\TransferException');
24+
}
25+
26+
function it_has_exceptions(TransferException $e, TransferException $e2)
27+
{
28+
$this->getExceptions()->shouldReturn([$e]);
29+
$this->hasException($e)->shouldReturn(true);
30+
$this->hasException($e2)->shouldReturn(false);
31+
$this->hasExceptions()->shouldReturn(true);
32+
}
33+
34+
function it_has_responses(ResponseInterface $response, ResponseInterface $response2)
35+
{
36+
$this->getResponses()->shouldReturn([$response]);
37+
$this->hasResponse($response)->shouldReturn(true);
38+
$this->hasResponse($response2)->shouldReturn(false);
39+
$this->hasResponses()->shouldReturn(true);
40+
}
41+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class ClientExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request, ResponseInterface $response)
12+
{
13+
$response->getStatusCode()->willReturn(400);
14+
15+
$this->beConstructedWith('message', $request, $response);
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType('Http\Client\Exception\ClientException');
21+
}
22+
23+
function it_is_http_exception()
24+
{
25+
$this->shouldHaveType('Http\Client\Exception\HttpException');
26+
}
27+
}

spec/Exception/HttpExceptionSpec.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class HttpExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request, ResponseInterface $response)
12+
{
13+
$response->getStatusCode()->willReturn(400);
14+
15+
$this->beConstructedWith('message', $request, $response);
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType('Http\Client\Exception\HttpException');
21+
}
22+
23+
function it_is_request_exception()
24+
{
25+
$this->shouldHaveType('Http\Client\Exception\RequestException');
26+
}
27+
28+
function it_has_a_response(ResponseInterface $response)
29+
{
30+
$this->getResponse()->shouldReturn($response);
31+
}
32+
33+
function it_creates_a_client_exception(RequestInterface $request, ResponseInterface $response)
34+
{
35+
$request->getRequestTarget()->willReturn('/uri');
36+
$request->getMethod()->willReturn('GET');
37+
$response->getStatusCode()->willReturn(404);
38+
$response->getReasonPhrase()->willReturn('Not Found');
39+
40+
$e = $this->create($request, $response);
41+
42+
$e->shouldHaveType('Http\Client\Exception\ClientException');
43+
$e->getMessage()->shouldReturn('Client error [url] /uri [http method] GET [status code] 404 [reason phrase] Not Found');
44+
}
45+
46+
function it_creates_a_server_exception(RequestInterface $request, ResponseInterface $response)
47+
{
48+
$request->getRequestTarget()->willReturn('/uri');
49+
$request->getMethod()->willReturn('GET');
50+
$response->getStatusCode()->willReturn(500);
51+
$response->getReasonPhrase()->willReturn('Internal Server Error');
52+
53+
$e = $this->create($request, $response);
54+
55+
$e->shouldHaveType('Http\Client\Exception\ServerException');
56+
$e->getMessage()->shouldReturn('Server error [url] /uri [http method] GET [status code] 500 [reason phrase] Internal Server Error');
57+
}
58+
59+
function it_creates_an_http_exception(RequestInterface $request, ResponseInterface $response)
60+
{
61+
$request->getRequestTarget()->willReturn('/uri');
62+
$request->getMethod()->willReturn('GET');
63+
$response->getStatusCode()->willReturn(100);
64+
$response->getReasonPhrase()->willReturn('Continue');
65+
66+
$e = $this->create($request, $response);
67+
68+
$e->shouldHaveType('Http\Client\Exception\HttpException');
69+
$e->getMessage()->shouldReturn('Unsuccessful response [url] /uri [http method] GET [status code] 100 [reason phrase] Continue');
70+
}
71+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class NetworkExceptionSpec extends ObjectBehavior
9+
{
10+
function let(RequestInterface $request)
11+
{
12+
$this->beConstructedWith('message', $request);
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Client\Exception\NetworkException');
18+
}
19+
20+
function it_is_request_exception()
21+
{
22+
$this->shouldHaveType('Http\Client\Exception\RequestException');
23+
}
24+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Http\Client\Exception\RequestException;
6+
use Psr\Http\Message\RequestInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class RequestExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request)
12+
{
13+
$this->beConstructedWith('message', $request);
14+
}
15+
16+
function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Http\Client\Exception\RequestException');
19+
}
20+
21+
function it_has_a_request(RequestInterface $request)
22+
{
23+
$this->getRequest()->shouldReturn($request);
24+
}
25+
26+
function it_wraps_an_exception(RequestInterface $request)
27+
{
28+
$e = new \Exception('message');
29+
30+
$requestException = $this->wrapException($request, $e);
31+
32+
$requestException->getMessage()->shouldReturn('message');
33+
}
34+
35+
function it_does_not_wrap_if_request_exception(RequestInterface $request, RequestException $requestException)
36+
{
37+
$this->wrapException($request, $requestException)->shouldReturn($requestException);
38+
}
39+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class ServerExceptionSpec extends ObjectBehavior
10+
{
11+
function let(RequestInterface $request, ResponseInterface $response)
12+
{
13+
$response->getStatusCode()->willReturn(500);
14+
15+
$this->beConstructedWith('message', $request, $response);
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType('Http\Client\Exception\ServerException');
21+
}
22+
23+
function it_is_http_exception()
24+
{
25+
$this->shouldHaveType('Http\Client\Exception\HttpException');
26+
}
27+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class TransferExceptionSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Http\Client\Exception\TransferException');
13+
}
14+
}

src/Exception/BatchException.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Http\Client\Exception;
4+
5+
/**
6+
* @author Márk Sági-Kazár <[email protected]>
7+
*/
8+
final class BatchException extends TransferException
9+
{
10+
/**
11+
* @var TransferException[]
12+
*/
13+
private $exceptions;
14+
15+
/**
16+
* @param TransferException[] $exceptions
17+
*/
18+
public function __construct(array $exceptions = [])
19+
{
20+
parent::__construct('An error occurred when sending multiple requests.');
21+
22+
foreach ($exceptions as $e) {
23+
if (!$e instanceof TransferException) {
24+
throw new InvalidArgumentException('Exception is not an instanceof Http\Client\Exception\TransferException');
25+
}
26+
}
27+
28+
$this->exceptions = $exceptions;
29+
}
30+
31+
/**
32+
* Returns all exceptions
33+
*
34+
* @return TransferException[]
35+
*/
36+
public function getExceptions()
37+
{
38+
return $this->exceptions;
39+
}
40+
41+
/**
42+
* Checks if a specific exception exists
43+
*
44+
* @param TransferException $exception
45+
*
46+
* @return boolean TRUE if there is the exception else FALSE.
47+
*/
48+
public function hasException(TransferException $exception)
49+
{
50+
return array_search($exception, $this->exceptions, true) !== false;
51+
}
52+
53+
/**
54+
* Checks if any exception exists
55+
*
56+
* @return boolean
57+
*/
58+
public function hasExceptions()
59+
{
60+
return !empty($this->exceptions);
61+
}
62+
}

src/Exception/ClientException.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Http\Client\Exception;
4+
5+
/**
6+
* Thrown when a client error (4xx) is encountered
7+
*
8+
* @author Márk Sági-Kazár <[email protected]>
9+
*/
10+
final class ClientException extends HttpException
11+
{
12+
}

0 commit comments

Comments
 (0)