From 81ea240a1f615bc05baea9acd47fe3b8a894e32a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 27 Aug 2015 00:38:18 +0200 Subject: [PATCH 01/11] Refactor exceptions, add new exceptions, make batch exception final, make all exceptions immutable --- src/Exception/BatchException.php | 97 +++++++++ src/Exception/ClientException.php | 13 ++ src/Exception/HttpClientException.php | 82 -------- src/Exception/HttpException.php | 48 +++++ src/Exception/MultiHttpClientException.php | 225 --------------------- src/Exception/NetworkException.php | 13 ++ src/Exception/RequestException.php | 40 ++++ src/Exception/ServerException.php | 13 ++ src/Exception/TransferException.php | 13 ++ 9 files changed, 237 insertions(+), 307 deletions(-) create mode 100644 src/Exception/BatchException.php create mode 100644 src/Exception/ClientException.php delete mode 100644 src/Exception/HttpClientException.php create mode 100644 src/Exception/HttpException.php delete mode 100644 src/Exception/MultiHttpClientException.php create mode 100644 src/Exception/NetworkException.php create mode 100644 src/Exception/RequestException.php create mode 100644 src/Exception/ServerException.php create mode 100644 src/Exception/TransferException.php diff --git a/src/Exception/BatchException.php b/src/Exception/BatchException.php new file mode 100644 index 0000000..99c11a1 --- /dev/null +++ b/src/Exception/BatchException.php @@ -0,0 +1,97 @@ + + */ +final class BatchException extends TransferException +{ + /** + * @var TransferException[] + */ + private $exceptions; + + /** + * @var ResponseInterface[] + */ + private $responses; + + /** + * @param TransferException[] $exceptions + * @param ResponseInterface[] $responses + */ + public function __construct(array $exceptions = [], array $responses = []) + { + parent::__construct('An error occurred when sending multiple requests.'); + + $this->setExceptions($exceptions); + $this->setResponses($responses); + } + + /** + * Returns all exceptions + * + * @return TransferException[] + */ + public function getExceptions() + { + return $this->exceptions; + } + + /** + * Checks if a specific exception exists + * + * @param TransferException $exception + * + * @return boolean TRUE if there is the exception else FALSE. + */ + public function hasException(TransferException $exception) + { + return array_search($exception, $this->exceptions, true) !== false; + } + + /** + * Checks if any exception exists + * + * @return boolean + */ + public function hasExceptions() + { + return !empty($this->exceptions); + } + + /** + * Returns all responses + * + * @return ResponseInterface[] + */ + public function getResponses() + { + return $this->responses; + } + + /** + * Checks if a specific response exists + * + * @param ResponseInterface $response + * + * @return boolean + */ + public function hasResponse(ResponseInterface $response) + { + return array_search($response, $this->responses, true) !== false; + } + + /** + * Checks if any response exists + * + * @return boolean + */ + public function hasResponses() + { + return !empty($this->responses); + } +} diff --git a/src/Exception/ClientException.php b/src/Exception/ClientException.php new file mode 100644 index 0000000..88f5667 --- /dev/null +++ b/src/Exception/ClientException.php @@ -0,0 +1,13 @@ + + */ +class ClientException extends HttpException +{ + +} diff --git a/src/Exception/HttpClientException.php b/src/Exception/HttpClientException.php deleted file mode 100644 index 97a6b99..0000000 --- a/src/Exception/HttpClientException.php +++ /dev/null @@ -1,82 +0,0 @@ - - */ -class HttpClientException extends RuntimeException -{ - /** - * @var RequestInterface|null - */ - private $request; - - /** - * @var ResponseInterface|null - */ - private $response; - - /** - * Returns the request - * - * @return RequestInterface|null - */ - public function getRequest() - { - return $this->request; - } - - /** - * Checks if there is a request - * - * @return boolean - */ - public function hasRequest() - { - return isset($this->request); - } - - /** - * Sets the request - * - * @param RequestInterface|null $request - */ - public function setRequest(RequestInterface $request = null) - { - $this->request = $request; - } - - /** - * Returns the response - * - * @return ResponseInterface|null - */ - public function getResponse() - { - return $this->response; - } - - /** - * Checks if there is a response - * - * @return boolean - */ - public function hasResponse() - { - return isset($this->response); - } - - /** - * Sets the response - * - * @param ResponseInterface|null $response - */ - public function setResponse(ResponseInterface $response = null) - { - $this->response = $response; - } -} diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php new file mode 100644 index 0000000..2fff7b6 --- /dev/null +++ b/src/Exception/HttpException.php @@ -0,0 +1,48 @@ + + */ +class HttpException extends RequestException +{ + /** + * @var ResponseInterface + */ + protected $response; + + /** + * @param string $message + * @param RequestInterface $request + * @param ResponseInterface $response + * @param \Exception|null $previous + */ + public function __construct( + $message, + RequestInterface $request, + ResponseInterface $response, + \Exception $previous = null + ) { + $this->response = $response; + + parent::__construct($message, $request, $previous); + + $this->code = $response->getStatusCode(); + } + + /** + * Returns the response + * + * @return ResponseInterface + */ + public function getResponse() + { + return $this->response; + } +} diff --git a/src/Exception/MultiHttpClientException.php b/src/Exception/MultiHttpClientException.php deleted file mode 100644 index 64456f2..0000000 --- a/src/Exception/MultiHttpClientException.php +++ /dev/null @@ -1,225 +0,0 @@ - - */ -class MultiHttpClientException extends RuntimeException -{ - /** - * @var HttpClientException[] - */ - private $exceptions; - - /** - * @var ResponseInterface[] - */ - private $responses; - - /** - * @param HttpClientException[] $exceptions - * @param ResponseInterface[] $responses - */ - public function __construct(array $exceptions = [], array $responses = []) - { - parent::__construct('An error occurred when sending multiple requests.'); - - $this->setExceptions($exceptions); - $this->setResponses($responses); - } - - /** - * Returns all exceptions - * - * @return HttpClientException[] - */ - public function getExceptions() - { - return $this->exceptions; - } - - /** - * Checks if a specific exception exists - * - * @param HttpClientException $exception - * - * @return boolean TRUE if there is the exception else FALSE. - */ - public function hasException(HttpClientException $exception) - { - return array_search($exception, $this->exceptions, true) !== false; - } - - /** - * Checks if any exception exists - * - * @return boolean - */ - public function hasExceptions() - { - return !empty($this->exceptions); - } - - /** - * Sets the exceptions - * - * @param HttpClientException[] $exceptions - */ - public function setExceptions(array $exceptions) - { - $this->clearExceptions(); - $this->addExceptions($exceptions); - } - - /** - * Adds an exception - * - * @param HttpClientException $exception - */ - public function addException(HttpClientException $exception) - { - $this->exceptions[] = $exception; - } - - /** - * Adds some exceptions - * - * @param HttpClientException[] $exceptions - */ - public function addExceptions(array $exceptions) - { - foreach ($exceptions as $exception) { - $this->addException($exception); - } - } - - /** - * Removes an exception - * - * @param HttpClientException $exception - */ - public function removeException(HttpClientException $exception) - { - unset($this->exceptions[array_search($exception, $this->exceptions, true)]); - $this->exceptions = array_values($this->exceptions); - } - - /** - * Removes some exceptions - * - * @param HttpClientException[] $exceptions - */ - public function removeExceptions(array $exceptions) - { - foreach ($exceptions as $exception) { - $this->removeException($exception); - } - } - - /** - * Clears all exceptions - */ - public function clearExceptions() - { - $this->exceptions = []; - } - - /** - * Returns all responses - * - * @return ResponseInterface[] - */ - public function getResponses() - { - return $this->responses; - } - - /** - * Checks if a specific response exists - * - * @param ResponseInterface $response - * - * @return boolean - */ - public function hasResponse(ResponseInterface $response) - { - return array_search($response, $this->responses, true) !== false; - } - - /** - * Checks if any response exists - * - * @return boolean - */ - public function hasResponses() - { - return !empty($this->responses); - } - - /** - * Sets the responses - * - * @param ResponseInterface[] $responses - */ - public function setResponses(array $responses) - { - $this->clearResponses(); - $this->addResponses($responses); - } - - /** - * Adds a response - * - * @param ResponseInterface $response - */ - public function addResponse(ResponseInterface $response) - { - $this->responses[] = $response; - } - - /** - * Adds some responses - * - * @param ResponseInterface[] $responses - */ - public function addResponses(array $responses) - { - foreach ($responses as $response) { - $this->addResponse($response); - } - } - - /** - * Removes a response - * - * @param ResponseInterface $response - */ - public function removeResponse(ResponseInterface $response) - { - unset($this->responses[array_search($response, $this->responses, true)]); - $this->responses = array_values($this->responses); - } - - /** - * Removes some responses - * - * @param ResponseInterface[] $responses - */ - public function removeResponses(array $responses) - { - foreach ($responses as $response) { - $this->removeResponse($response); - } - } - - /** - * Clears all responses - */ - public function clearResponses() - { - $this->responses = []; - } -} diff --git a/src/Exception/NetworkException.php b/src/Exception/NetworkException.php new file mode 100644 index 0000000..a38857e --- /dev/null +++ b/src/Exception/NetworkException.php @@ -0,0 +1,13 @@ + + */ +class NetworkException extends RequestException +{ + +} diff --git a/src/Exception/RequestException.php b/src/Exception/RequestException.php new file mode 100644 index 0000000..ce29f89 --- /dev/null +++ b/src/Exception/RequestException.php @@ -0,0 +1,40 @@ + + */ +class RequestException extends TransferException +{ + /** + * @var RequestInterface + */ + protected $request; + + /** + * @param string $message + * @param RequestInterface $request + * @param \Exception|null $previous + */ + public function __construct($message, RequestInterface $request, \Exception $previous = null) + { + $this->request = $request; + + parent::__construct($message, 0, $previous); + } + + /** + * Returns the request + * + * @return RequestInterface + */ + public function getRequest() + { + return $this->request; + } +} diff --git a/src/Exception/ServerException.php b/src/Exception/ServerException.php new file mode 100644 index 0000000..56eec84 --- /dev/null +++ b/src/Exception/ServerException.php @@ -0,0 +1,13 @@ + + */ +class ServerException extends HttpException +{ + +} diff --git a/src/Exception/TransferException.php b/src/Exception/TransferException.php new file mode 100644 index 0000000..7530975 --- /dev/null +++ b/src/Exception/TransferException.php @@ -0,0 +1,13 @@ + + */ +class TransferException extends RuntimeException +{ + +} From e341e16beace16cab563412827d4e8c90f5b5b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 27 Aug 2015 01:14:51 +0200 Subject: [PATCH 02/11] Add factory methods --- src/Exception/HttpException.php | 36 ++++++++++++++++++++++++++++++ src/Exception/RequestException.php | 15 +++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php index 2fff7b6..1ac528f 100644 --- a/src/Exception/HttpException.php +++ b/src/Exception/HttpException.php @@ -45,4 +45,40 @@ public function getResponse() { return $this->response; } + + /** + * Factory method to create a new exception with a normalized error message + * + * @param RequestInterface $request + * @param ResponseInterface $response + * @param \Exception|null $previous + * + * @return HttpException + */ + public static function create(RequestInterface $request, ResponseInterface $response, \Exception $previous = null) + { + $code = floor($response->getStatusCode() / 100); + + if ($code == '4') { + $message = 'Client error'; + $className = __NAMESPACE__ . '\\ClientException'; + } elseif ($code == '5') { + $message = 'Server error'; + $className = __NAMESPACE__ . '\\ServerException'; + } else { + $message = 'Unsuccessful response'; + $className = __CLASS__; + } + + $message = sprintf( + '%s [url] %s [http method] %s [status code] %s [reason phrase] %', + $message, + $request->getRequestTarget(), + $request->getMethod(), + $response->getStatusCode(), + $response->getReasonPhrase() + ); + + return new $className($message, $request, $response, $previous); + } } diff --git a/src/Exception/RequestException.php b/src/Exception/RequestException.php index ce29f89..fdf8332 100644 --- a/src/Exception/RequestException.php +++ b/src/Exception/RequestException.php @@ -37,4 +37,19 @@ public function getRequest() { return $this->request; } + + /** + * @param RequestInterface $request + * @param \Exception $e + * + * @return RequestException + */ + public static function wrapException(RequestInterface $request, \Exception $e) + { + if (!$e instanceof RequestException) { + $e = new RequestException($e->getMessage(), $request, $e); + } + + return $e; + } } From 6dc544b1180d84e88c3d8de0df3e556042aab6dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 27 Aug 2015 15:18:37 +0200 Subject: [PATCH 03/11] Add tests for exceptions, fix some mistakes --- spec/Exception/BatchExceptionSpec.php | 41 ++++++++++++++ spec/Exception/ClientExceptionSpec.php | 27 +++++++++ spec/Exception/HttpExceptionSpec.php | 71 ++++++++++++++++++++++++ spec/Exception/NetworkExceptionSpec.php | 24 ++++++++ spec/Exception/RequestExceptionSpec.php | 39 +++++++++++++ spec/Exception/ServerExceptionSpec.php | 27 +++++++++ spec/Exception/TransferExceptionSpec.php | 14 +++++ src/Exception/BatchException.php | 17 +++++- src/Exception/HttpException.php | 2 +- 9 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 spec/Exception/BatchExceptionSpec.php create mode 100644 spec/Exception/ClientExceptionSpec.php create mode 100644 spec/Exception/HttpExceptionSpec.php create mode 100644 spec/Exception/NetworkExceptionSpec.php create mode 100644 spec/Exception/RequestExceptionSpec.php create mode 100644 spec/Exception/ServerExceptionSpec.php create mode 100644 spec/Exception/TransferExceptionSpec.php diff --git a/spec/Exception/BatchExceptionSpec.php b/spec/Exception/BatchExceptionSpec.php new file mode 100644 index 0000000..f8cc048 --- /dev/null +++ b/spec/Exception/BatchExceptionSpec.php @@ -0,0 +1,41 @@ +beConstructedWith([$e], [$response]); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Exception\BatchException'); + } + + function it_is_a_transfer_exception() + { + $this->shouldHaveType('Http\Client\Exception\TransferException'); + } + + function it_has_exceptions(TransferException $e, TransferException $e2) + { + $this->getExceptions()->shouldReturn([$e]); + $this->hasException($e)->shouldReturn(true); + $this->hasException($e2)->shouldReturn(false); + $this->hasExceptions()->shouldReturn(true); + } + + function it_has_responses(ResponseInterface $response, ResponseInterface $response2) + { + $this->getResponses()->shouldReturn([$response]); + $this->hasResponse($response)->shouldReturn(true); + $this->hasResponse($response2)->shouldReturn(false); + $this->hasResponses()->shouldReturn(true); + } +} diff --git a/spec/Exception/ClientExceptionSpec.php b/spec/Exception/ClientExceptionSpec.php new file mode 100644 index 0000000..706f62f --- /dev/null +++ b/spec/Exception/ClientExceptionSpec.php @@ -0,0 +1,27 @@ +getStatusCode()->willReturn(400); + + $this->beConstructedWith('message', $request, $response); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Exception\ClientException'); + } + + function it_is_http_exception() + { + $this->shouldHaveType('Http\Client\Exception\HttpException'); + } +} diff --git a/spec/Exception/HttpExceptionSpec.php b/spec/Exception/HttpExceptionSpec.php new file mode 100644 index 0000000..9bd4392 --- /dev/null +++ b/spec/Exception/HttpExceptionSpec.php @@ -0,0 +1,71 @@ +getStatusCode()->willReturn(400); + + $this->beConstructedWith('message', $request, $response); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Exception\HttpException'); + } + + function it_is_request_exception() + { + $this->shouldHaveType('Http\Client\Exception\RequestException'); + } + + function it_has_a_response(ResponseInterface $response) + { + $this->getResponse()->shouldReturn($response); + } + + function it_creates_a_client_exception(RequestInterface $request, ResponseInterface $response) + { + $request->getRequestTarget()->willReturn('/uri'); + $request->getMethod()->willReturn('GET'); + $response->getStatusCode()->willReturn(404); + $response->getReasonPhrase()->willReturn('Not Found'); + + $e = $this->create($request, $response); + + $e->shouldHaveType('Http\Client\Exception\ClientException'); + $e->getMessage()->shouldReturn('Client error [url] /uri [http method] GET [status code] 404 [reason phrase] Not Found'); + } + + function it_creates_a_server_exception(RequestInterface $request, ResponseInterface $response) + { + $request->getRequestTarget()->willReturn('/uri'); + $request->getMethod()->willReturn('GET'); + $response->getStatusCode()->willReturn(500); + $response->getReasonPhrase()->willReturn('Internal Server Error'); + + $e = $this->create($request, $response); + + $e->shouldHaveType('Http\Client\Exception\ServerException'); + $e->getMessage()->shouldReturn('Server error [url] /uri [http method] GET [status code] 500 [reason phrase] Internal Server Error'); + } + + function it_creates_an_http_exception(RequestInterface $request, ResponseInterface $response) + { + $request->getRequestTarget()->willReturn('/uri'); + $request->getMethod()->willReturn('GET'); + $response->getStatusCode()->willReturn(100); + $response->getReasonPhrase()->willReturn('Continue'); + + $e = $this->create($request, $response); + + $e->shouldHaveType('Http\Client\Exception\HttpException'); + $e->getMessage()->shouldReturn('Unsuccessful response [url] /uri [http method] GET [status code] 100 [reason phrase] Continue'); + } +} diff --git a/spec/Exception/NetworkExceptionSpec.php b/spec/Exception/NetworkExceptionSpec.php new file mode 100644 index 0000000..a8e0eb6 --- /dev/null +++ b/spec/Exception/NetworkExceptionSpec.php @@ -0,0 +1,24 @@ +beConstructedWith('message', $request); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Exception\NetworkException'); + } + + function it_is_request_exception() + { + $this->shouldHaveType('Http\Client\Exception\RequestException'); + } +} diff --git a/spec/Exception/RequestExceptionSpec.php b/spec/Exception/RequestExceptionSpec.php new file mode 100644 index 0000000..9779280 --- /dev/null +++ b/spec/Exception/RequestExceptionSpec.php @@ -0,0 +1,39 @@ +beConstructedWith('message', $request); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Exception\RequestException'); + } + + function it_has_a_request(RequestInterface $request) + { + $this->getRequest()->shouldReturn($request); + } + + function it_wraps_an_exception(RequestInterface $request) + { + $e = new \Exception('message'); + + $requestException = $this->wrapException($request, $e); + + $requestException->getMessage()->shouldReturn('message'); + } + + function it_does_not_wrap_if_request_exception(RequestInterface $request, RequestException $requestException) + { + $this->wrapException($request, $requestException)->shouldReturn($requestException); + } +} diff --git a/spec/Exception/ServerExceptionSpec.php b/spec/Exception/ServerExceptionSpec.php new file mode 100644 index 0000000..1005c23 --- /dev/null +++ b/spec/Exception/ServerExceptionSpec.php @@ -0,0 +1,27 @@ +getStatusCode()->willReturn(500); + + $this->beConstructedWith('message', $request, $response); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Exception\ServerException'); + } + + function it_is_http_exception() + { + $this->shouldHaveType('Http\Client\Exception\HttpException'); + } +} diff --git a/spec/Exception/TransferExceptionSpec.php b/spec/Exception/TransferExceptionSpec.php new file mode 100644 index 0000000..bd5d39f --- /dev/null +++ b/spec/Exception/TransferExceptionSpec.php @@ -0,0 +1,14 @@ +shouldHaveType('Http\Client\Exception\TransferException'); + } +} diff --git a/src/Exception/BatchException.php b/src/Exception/BatchException.php index 99c11a1..f047ce3 100644 --- a/src/Exception/BatchException.php +++ b/src/Exception/BatchException.php @@ -27,8 +27,21 @@ public function __construct(array $exceptions = [], array $responses = []) { parent::__construct('An error occurred when sending multiple requests.'); - $this->setExceptions($exceptions); - $this->setResponses($responses); + foreach ($exceptions as $e) { + if (!$e instanceof TransferException) { + throw new InvalidArgumentException('Exception is not an instanceof Http\Client\Exception\TransferException'); + } + } + + foreach ($responses as $response) { + if (!$response instanceof ResponseInterface) { + throw new InvalidArgumentException('Response is not an instanceof Psr\Http\Message\ResponseInterface'); + } + } + + + $this->exceptions = $exceptions; + $this->responses = $responses; } /** diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php index 1ac528f..9fbe74c 100644 --- a/src/Exception/HttpException.php +++ b/src/Exception/HttpException.php @@ -71,7 +71,7 @@ public static function create(RequestInterface $request, ResponseInterface $resp } $message = sprintf( - '%s [url] %s [http method] %s [status code] %s [reason phrase] %', + '%s [url] %s [http method] %s [status code] %s [reason phrase] %s', $message, $request->getRequestTarget(), $request->getMethod(), From 916048db6f697418ec50cd44dc921f62309e0548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Sat, 19 Sep 2015 19:52:37 +0200 Subject: [PATCH 04/11] Fix indentation --- src/Exception/BatchException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exception/BatchException.php b/src/Exception/BatchException.php index f047ce3..0cfdc05 100644 --- a/src/Exception/BatchException.php +++ b/src/Exception/BatchException.php @@ -21,7 +21,7 @@ final class BatchException extends TransferException /** * @param TransferException[] $exceptions - * @param ResponseInterface[] $responses + * @param ResponseInterface[] $responses */ public function __construct(array $exceptions = [], array $responses = []) { From c54bbf1000b4de482468ec83e46389503f1da396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Sat, 19 Sep 2015 19:56:02 +0200 Subject: [PATCH 05/11] Fix exception descriptions --- src/Exception/HttpException.php | 4 +++- src/Exception/RequestException.php | 4 +++- src/Exception/TransferException.php | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php index 9fbe74c..75218c0 100644 --- a/src/Exception/HttpException.php +++ b/src/Exception/HttpException.php @@ -6,7 +6,9 @@ use Psr\Http\Message\ResponseInterface; /** - * Thrown when both a request and a response are available + * Thrown when a response was received but has an error status code. + * + * This exception always provides the request and response objects. * * @author Márk Sági-Kazár */ diff --git a/src/Exception/RequestException.php b/src/Exception/RequestException.php index fdf8332..d3d6596 100644 --- a/src/Exception/RequestException.php +++ b/src/Exception/RequestException.php @@ -5,7 +5,9 @@ use Psr\Http\Message\RequestInterface; /** - * Thrown when a request is available + * Base exception for when a request failed. + * + * This can be because of network problems or the status code of the response. * * @author Márk Sági-Kazár */ diff --git a/src/Exception/TransferException.php b/src/Exception/TransferException.php index 7530975..95a2a72 100644 --- a/src/Exception/TransferException.php +++ b/src/Exception/TransferException.php @@ -3,7 +3,7 @@ namespace Http\Client\Exception; /** - * Thrown when an error related to sending/receiving the request/response occurs + * Base exception for all php-http exceptions. * * @author Márk Sági-Kazár */ From 895872d7db6a18b594d39784e12448946f47dfef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Sat, 19 Sep 2015 19:57:37 +0200 Subject: [PATCH 06/11] Fix PSR-2 incompatibilities --- src/Exception/ClientException.php | 1 - src/Exception/InvalidArgumentException.php | 1 - src/Exception/NetworkException.php | 1 - src/Exception/RuntimeException.php | 1 - src/Exception/ServerException.php | 1 - src/Exception/TransferException.php | 1 - 6 files changed, 6 deletions(-) diff --git a/src/Exception/ClientException.php b/src/Exception/ClientException.php index 88f5667..af16fe1 100644 --- a/src/Exception/ClientException.php +++ b/src/Exception/ClientException.php @@ -9,5 +9,4 @@ */ class ClientException extends HttpException { - } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index e761c49..9d9f938 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -11,5 +11,4 @@ */ class InvalidArgumentException extends \InvalidArgumentException implements Exception { - } diff --git a/src/Exception/NetworkException.php b/src/Exception/NetworkException.php index a38857e..21ea391 100644 --- a/src/Exception/NetworkException.php +++ b/src/Exception/NetworkException.php @@ -9,5 +9,4 @@ */ class NetworkException extends RequestException { - } diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index b8a7fc5..4bffb40 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -11,5 +11,4 @@ */ class RuntimeException extends \RuntimeException implements Exception { - } diff --git a/src/Exception/ServerException.php b/src/Exception/ServerException.php index 56eec84..9ed8fd8 100644 --- a/src/Exception/ServerException.php +++ b/src/Exception/ServerException.php @@ -9,5 +9,4 @@ */ class ServerException extends HttpException { - } diff --git a/src/Exception/TransferException.php b/src/Exception/TransferException.php index 95a2a72..b8a7ec7 100644 --- a/src/Exception/TransferException.php +++ b/src/Exception/TransferException.php @@ -9,5 +9,4 @@ */ class TransferException extends RuntimeException { - } From a943f2d2966d634cd988405a6a825064a6d8fc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Sat, 19 Sep 2015 19:59:23 +0200 Subject: [PATCH 07/11] Fix logical issues in code --- src/Exception/HttpException.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php index 75218c0..5e47b07 100644 --- a/src/Exception/HttpException.php +++ b/src/Exception/HttpException.php @@ -32,10 +32,10 @@ public function __construct( \Exception $previous = null ) { $this->response = $response; + $this->code = $response->getStatusCode(); - parent::__construct($message, $request, $previous); - $this->code = $response->getStatusCode(); + parent::__construct($message, $request, $previous); } /** @@ -59,12 +59,12 @@ public function getResponse() */ public static function create(RequestInterface $request, ResponseInterface $response, \Exception $previous = null) { - $code = floor($response->getStatusCode() / 100); + $code = $response->getStatusCode(); - if ($code == '4') { + if ($code >= 400 && $code < 500) { $message = 'Client error'; $className = __NAMESPACE__ . '\\ClientException'; - } elseif ($code == '5') { + } elseif ($code >= 500 && $code < 600) { $message = 'Server error'; $className = __NAMESPACE__ . '\\ServerException'; } else { From c630ab7b71d097d5d98399a59d275094cf64936a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Mon, 21 Sep 2015 12:15:49 +0200 Subject: [PATCH 08/11] Make request property final --- src/Exception/RequestException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exception/RequestException.php b/src/Exception/RequestException.php index d3d6596..c4e74a3 100644 --- a/src/Exception/RequestException.php +++ b/src/Exception/RequestException.php @@ -16,7 +16,7 @@ class RequestException extends TransferException /** * @var RequestInterface */ - protected $request; + private $request; /** * @param string $message From 4174e1a242b368af81bc5e1ad70a7cee275aebc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Mon, 21 Sep 2015 12:17:28 +0200 Subject: [PATCH 09/11] Make exceptions final --- src/Exception/ClientException.php | 2 +- src/Exception/NetworkException.php | 2 +- src/Exception/ServerException.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Exception/ClientException.php b/src/Exception/ClientException.php index af16fe1..454e386 100644 --- a/src/Exception/ClientException.php +++ b/src/Exception/ClientException.php @@ -7,6 +7,6 @@ * * @author Márk Sági-Kazár */ -class ClientException extends HttpException +final class ClientException extends HttpException { } diff --git a/src/Exception/NetworkException.php b/src/Exception/NetworkException.php index 21ea391..c916d17 100644 --- a/src/Exception/NetworkException.php +++ b/src/Exception/NetworkException.php @@ -7,6 +7,6 @@ * * @author Márk Sági-Kazár */ -class NetworkException extends RequestException +final class NetworkException extends RequestException { } diff --git a/src/Exception/ServerException.php b/src/Exception/ServerException.php index 9ed8fd8..42c7b4b 100644 --- a/src/Exception/ServerException.php +++ b/src/Exception/ServerException.php @@ -7,6 +7,6 @@ * * @author Márk Sági-Kazár */ -class ServerException extends HttpException +final class ServerException extends HttpException { } From df3bdbb90f7dab4edb8e3acaef871371395069e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Mon, 21 Sep 2015 13:10:17 +0200 Subject: [PATCH 10/11] Revert NetworkException: not final --- src/Exception/NetworkException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exception/NetworkException.php b/src/Exception/NetworkException.php index c916d17..21ea391 100644 --- a/src/Exception/NetworkException.php +++ b/src/Exception/NetworkException.php @@ -7,6 +7,6 @@ * * @author Márk Sági-Kazár */ -final class NetworkException extends RequestException +class NetworkException extends RequestException { } From 58a3d52492090bda2b0fd9c1830f8b838ca62af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=CC=81gi-Kaza=CC=81r=20Ma=CC=81rk?= Date: Mon, 21 Sep 2015 13:55:36 +0200 Subject: [PATCH 11/11] Remove responses from BatchException --- src/Exception/BatchException.php | 52 ++------------------------------ 1 file changed, 2 insertions(+), 50 deletions(-) diff --git a/src/Exception/BatchException.php b/src/Exception/BatchException.php index 0cfdc05..1f71000 100644 --- a/src/Exception/BatchException.php +++ b/src/Exception/BatchException.php @@ -2,10 +2,8 @@ namespace Http\Client\Exception; -use Psr\Http\Message\ResponseInterface; - /** - * @author GeLo + * @author Márk Sági-Kazár */ final class BatchException extends TransferException { @@ -14,16 +12,10 @@ final class BatchException extends TransferException */ private $exceptions; - /** - * @var ResponseInterface[] - */ - private $responses; - /** * @param TransferException[] $exceptions - * @param ResponseInterface[] $responses */ - public function __construct(array $exceptions = [], array $responses = []) + public function __construct(array $exceptions = []) { parent::__construct('An error occurred when sending multiple requests.'); @@ -33,15 +25,7 @@ public function __construct(array $exceptions = [], array $responses = []) } } - foreach ($responses as $response) { - if (!$response instanceof ResponseInterface) { - throw new InvalidArgumentException('Response is not an instanceof Psr\Http\Message\ResponseInterface'); - } - } - - $this->exceptions = $exceptions; - $this->responses = $responses; } /** @@ -75,36 +59,4 @@ public function hasExceptions() { return !empty($this->exceptions); } - - /** - * Returns all responses - * - * @return ResponseInterface[] - */ - public function getResponses() - { - return $this->responses; - } - - /** - * Checks if a specific response exists - * - * @param ResponseInterface $response - * - * @return boolean - */ - public function hasResponse(ResponseInterface $response) - { - return array_search($response, $this->responses, true) !== false; - } - - /** - * Checks if any response exists - * - * @return boolean - */ - public function hasResponses() - { - return !empty($this->responses); - } }