Skip to content

Commit 45cb5d0

Browse files
committed
Refactor exceptions, add new exceptions, make batch exception final, make all exceptions immutable
1 parent d9b9043 commit 45cb5d0

9 files changed

+237
-307
lines changed

src/Exception/BatchException.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Http\Client\Exception;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
7+
/**
8+
* @author GeLo <[email protected]>
9+
*/
10+
final class BatchException extends TransferException
11+
{
12+
/**
13+
* @var TransferException[]
14+
*/
15+
private $exceptions;
16+
17+
/**
18+
* @var ResponseInterface[]
19+
*/
20+
private $responses;
21+
22+
/**
23+
* @param TransferException[] $exceptions
24+
* @param ResponseInterface[] $responses
25+
*/
26+
public function __construct(array $exceptions = [], array $responses = [])
27+
{
28+
parent::__construct('An error occurred when sending multiple requests.');
29+
30+
$this->setExceptions($exceptions);
31+
$this->setResponses($responses);
32+
}
33+
34+
/**
35+
* Returns all exceptions
36+
*
37+
* @return TransferException[]
38+
*/
39+
public function getExceptions()
40+
{
41+
return $this->exceptions;
42+
}
43+
44+
/**
45+
* Checks if a specific exception exists
46+
*
47+
* @param TransferException $exception
48+
*
49+
* @return boolean TRUE if there is the exception else FALSE.
50+
*/
51+
public function hasException(TransferException $exception)
52+
{
53+
return array_search($exception, $this->exceptions, true) !== false;
54+
}
55+
56+
/**
57+
* Checks if any exception exists
58+
*
59+
* @return boolean
60+
*/
61+
public function hasExceptions()
62+
{
63+
return !empty($this->exceptions);
64+
}
65+
66+
/**
67+
* Returns all responses
68+
*
69+
* @return ResponseInterface[]
70+
*/
71+
public function getResponses()
72+
{
73+
return $this->responses;
74+
}
75+
76+
/**
77+
* Checks if a specific response exists
78+
*
79+
* @param ResponseInterface $response
80+
*
81+
* @return boolean
82+
*/
83+
public function hasResponse(ResponseInterface $response)
84+
{
85+
return array_search($response, $this->responses, true) !== false;
86+
}
87+
88+
/**
89+
* Checks if any response exists
90+
*
91+
* @return boolean
92+
*/
93+
public function hasResponses()
94+
{
95+
return !empty($this->responses);
96+
}
97+
}

src/Exception/ClientException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
class ClientException extends HttpException
11+
{
12+
13+
}

src/Exception/HttpClientException.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/Exception/HttpException.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Http\Client\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
/**
9+
* Thrown when both a request and a response are available
10+
*
11+
* @author Márk Sági-Kazár <[email protected]>
12+
*/
13+
class HttpException extends RequestException
14+
{
15+
/**
16+
* @var ResponseInterface
17+
*/
18+
protected $response;
19+
20+
/**
21+
* @param string $message
22+
* @param RequestInterface $request
23+
* @param ResponseInterface $response
24+
* @param \Exception|null $previous
25+
*/
26+
public function __construct(
27+
$message,
28+
RequestInterface $request,
29+
ResponseInterface $response,
30+
\Exception $previous = null
31+
) {
32+
$this->response = $response;
33+
34+
parent::__construct($message, $request, $previous);
35+
36+
$this->code = $response->getStatusCode();
37+
}
38+
39+
/**
40+
* Returns the response
41+
*
42+
* @return ResponseInterface
43+
*/
44+
public function getResponse()
45+
{
46+
return $this->response;
47+
}
48+
}

0 commit comments

Comments
 (0)