Skip to content

Commit 36c7f09

Browse files
committed
Create specific exception for each error
1 parent 8207227 commit 36c7f09

File tree

7 files changed

+58
-18
lines changed

7 files changed

+58
-18
lines changed

src/Client.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Http\Client\Exception\NetworkException;
66
use Http\Client\HttpClient;
7+
use Http\Client\Socket\Exception\ConnectionException;
8+
use Http\Client\Socket\Exception\InvalidRequestException;
9+
use Http\Client\Socket\Exception\SSLConnectionException;
710
use Http\Discovery\MessageFactoryDiscovery;
811
use Http\Message\ResponseFactory;
912
use Psr\Http\Message\RequestInterface;
@@ -98,7 +101,7 @@ public function sendRequest(RequestInterface $request)
98101
* @param string $remote Entrypoint for the connection
99102
* @param bool $useSsl Whether to use ssl or not
100103
*
101-
* @throws NetworkException When the connection fail
104+
* @throws ConnectionException|SSLConnectionException When the connection fail
102105
*
103106
* @return resource Socket resource
104107
*/
@@ -109,15 +112,13 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl)
109112
$socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']);
110113

111114
if (false === $socket) {
112-
throw new NetworkException($errMsg, $request);
115+
throw new ConnectionException($errMsg, $request);
113116
}
114117

115118
stream_set_timeout($socket, floor($this->config['timeout'] / 1000), $this->config['timeout'] % 1000);
116119

117-
if ($useSsl) {
118-
if (false === @stream_socket_enable_crypto($socket, true, $this->config['ssl_method'])) {
119-
throw new NetworkException(sprintf('Cannot enable tls: %s', error_get_last()['message']), $request);
120-
}
120+
if ($useSsl && false === @stream_socket_enable_crypto($socket, true, $this->config['ssl_method'])) {
121+
throw new SSLConnectionException(sprintf('Cannot enable tls: %s', error_get_last()['message']), $request);
121122
}
122123

123124
return $socket;
@@ -163,18 +164,18 @@ protected function configure(array $config = [])
163164
*
164165
* @param RequestInterface $request
165166
*
166-
* @throws NetworkException When no remote can be determined from the request
167+
* @throws InvalidRequestException When no remote can be determined from the request
167168
*
168169
* @return string
169170
*/
170171
private function determineRemoteFromRequest(RequestInterface $request)
171172
{
172-
if ($request->getUri()->getHost() == '' && !$request->hasHeader('Host')) {
173-
throw new NetworkException('Cannot find connection endpoint for this request', $request);
173+
if (!$request->hasHeader('Host') && $request->getUri()->getHost() === '') {
174+
throw new InvalidRequestException('Remote is not defined and we cannot determine a connection endpoint for this request (no Host header)', $request);
174175
}
175176

176177
$host = $request->getUri()->getHost();
177-
$port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() == 'https' ? 443 : 80);
178+
$port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() === 'https' ? 443 : 80);
178179
$endpoint = sprintf('%s:%s', $host, $port);
179180

180181
// If use the host header if present for the endpoint

src/Exception/BrokenPipeException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Http\Client\Socket\Exception;
4+
5+
use Http\Client\Exception\NetworkException;
6+
7+
class BrokenPipeException extends NetworkException
8+
{
9+
}

src/Exception/ConnectionException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Http\Client\Socket\Exception;
4+
5+
use Http\Client\Exception\NetworkException;
6+
7+
class ConnectionException extends NetworkException
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Http\Client\Socket\Exception;
4+
5+
use Http\Client\Exception\NetworkException;
6+
7+
class InvalidRequestException extends NetworkException
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Http\Client\Socket\Exception;
4+
5+
use Http\Client\Exception\NetworkException;
6+
7+
class SSLConnectionException extends NetworkException
8+
{
9+
}

src/RequestWriter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Client\Socket;
44

55
use Http\Client\Exception\NetworkException;
6+
use Http\Client\Socket\Exception\BrokenPipeException;
67
use Psr\Http\Message\RequestInterface;
78

89
/**
@@ -21,12 +22,12 @@ trait RequestWriter
2122
* @param RequestInterface $request
2223
* @param int $bufferSize
2324
*
24-
* @throws \Http\Client\Exception\NetworkException
25+
* @throws BrokenPipeException
2526
*/
2627
protected function writeRequest($socket, RequestInterface $request, $bufferSize = 8192)
2728
{
2829
if (false === $this->fwrite($socket, $this->transformRequestHeadersToString($request))) {
29-
throw new NetworkException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
30+
throw new BrokenPipeException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
3031
}
3132

3233
if ($request->getBody()->isReadable()) {
@@ -41,8 +42,7 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize
4142
* @param RequestInterface $request
4243
* @param int $bufferSize
4344
*
44-
* @throws \Http\Client\Exception\NetworkException
45-
* @throws \Http\Client\Exception\RequestException
45+
* @throws BrokenPipeException
4646
*/
4747
protected function writeBody($socket, RequestInterface $request, $bufferSize = 8192)
4848
{
@@ -56,7 +56,7 @@ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8
5656
$buffer = $body->read($bufferSize);
5757

5858
if (false === $this->fwrite($socket, $buffer)) {
59-
throw new NetworkException('An error occur when writing request to client (BROKEN EPIPE)', $request);
59+
throw new BrokenPipeException('An error occur when writing request to client (BROKEN EPIPE)', $request);
6060
}
6161
}
6262
}

src/ResponseReader.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Http\Client\Socket;
44

55
use Http\Client\Exception\NetworkException;
6+
use Http\Client\Socket\Exception\BrokenPipeException;
7+
use Http\Client\Socket\Exception\TimeoutException;
68
use Http\Message\ResponseFactory;
79
use Psr\Http\Message\RequestInterface;
810
use Psr\Http\Message\ResponseInterface;
@@ -27,7 +29,8 @@ trait ResponseReader
2729
* @param RequestInterface $request
2830
* @param resource $socket
2931
*
30-
* @throws NetworkException When the response cannot be read
32+
* @throws TimeoutException When the socket timed out
33+
* @throws BrokenPipeException When the response cannot be read
3134
*
3235
* @return ResponseInterface
3336
*/
@@ -46,13 +49,13 @@ protected function readResponse(RequestInterface $request, $socket)
4649
$metadatas = stream_get_meta_data($socket);
4750

4851
if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) {
49-
throw new NetworkException('Error while reading response, stream timed out', $request);
52+
throw new TimeoutException('Error while reading response, stream timed out', $request);
5053
}
5154

5255
$parts = explode(' ', array_shift($headers), 3);
5356

5457
if (count($parts) <= 1) {
55-
throw new NetworkException('Cannot read the response', $request);
58+
throw new BrokenPipeException('Cannot read the response', $request);
5659
}
5760

5861
$protocol = substr($parts[0], -3);

0 commit comments

Comments
 (0)