Skip to content

Commit 44e8c01

Browse files
committed
Add custom UnexpectedValueException, throw exception when request is not found
1 parent 3eb7cbc commit 44e8c01

File tree

6 files changed

+50
-13
lines changed

6 files changed

+50
-13
lines changed

spec/BatchResultSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function it_has_a_response_for_a_request(RequestInterface $request, ResponseInte
3636
{
3737
$new = $this->addResponse($request, $response);
3838

39-
$this->shouldThrow('Http\Client\Exception\InvalidArgumentException')->duringGetResponseFor($request);
39+
$this->shouldThrow('Http\Client\Exception\UnexpectedValueException')->duringGetResponseFor($request);
4040
$this->hasResponseFor($request)->shouldReturn(false);
4141
$new->getResponseFor($request)->shouldReturn($response);
4242
$new->hasResponseFor($request)->shouldReturn(true);

spec/Exception/BatchExceptionSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function it_throws_an_exception_if_the_result_is_already_set()
3434

3535
function it_has_an_exception_for_a_request(RequestInterface $request, Exception $exception)
3636
{
37-
$this->shouldThrow('Http\Client\Exception\InvalidArgumentException')->duringGetExceptionFor($request);
37+
$this->shouldThrow('Http\Client\Exception\UnexpectedValueException')->duringGetExceptionFor($request);
3838
$this->hasExceptionFor($request)->shouldReturn(false);
3939

4040
$this->addException($request, $exception);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Exception;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
class UnexpectedValueExceptionSpec extends ObjectBehavior
8+
{
9+
function it_is_initializable()
10+
{
11+
$this->shouldHaveType('Http\Client\Exception\UnexpectedValueException');
12+
}
13+
14+
function it_is_invalid_argument_exception()
15+
{
16+
$this->shouldHaveType('UnexpectedValueException');
17+
}
18+
19+
function it_is_exception()
20+
{
21+
$this->shouldImplement('Http\Client\Exception');
22+
}
23+
}

src/BatchResult.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Http\Client;
44

5-
use Http\Client\Exception\InvalidArgumentException;
5+
use Http\Client\Exception\UnexpectedValueException;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88

@@ -46,15 +46,15 @@ public function getResponses()
4646
*
4747
* @return ResponseInterface
4848
*
49-
* @throws InvalidArgumentException
49+
* @throws UnexpectedValueException
5050
*/
5151
public function getResponseFor(RequestInterface $request)
5252
{
53-
if (!$this->responses->contains($request)) {
54-
throw new InvalidArgumentException('No response can be found for the given request');
53+
try {
54+
return $this->responses[$request];
55+
} catch (\UnexpectedValueException $e) {
56+
throw new UnexpectedValueException('Request not found', $e->getCode(), $e);
5557
}
56-
57-
return $this->responses[$request];
5858
}
5959

6060
/**

src/Exception/BatchException.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ public function getExceptions()
111111
*
112112
* @return Exception
113113
*
114-
* @throws InvalidArgumentException
114+
* @throws UnexpectedValueException
115115
*/
116116
public function getExceptionFor(RequestInterface $request)
117117
{
118-
if (!$this->exceptions->contains($request)) {
119-
throw new InvalidArgumentException('No exception can be found for the given request');
118+
try {
119+
return $this->exceptions[$request];
120+
} catch (\UnexpectedValueException $e) {
121+
throw new UnexpectedValueException('Request not found', $e->getCode(), $e);
120122
}
121-
122-
return $this->exceptions[$request];
123123
}
124124

125125
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Http\Client\Exception;
4+
5+
use Http\Client\Exception;
6+
7+
/**
8+
* Custom Unexpected Value exception
9+
*
10+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
11+
*/
12+
class UnexpectedValueException extends \UnexpectedValueException implements Exception
13+
{
14+
}

0 commit comments

Comments
 (0)