Skip to content

Commit a6bfe1b

Browse files
authored
Merge pull request #23 from php-http/feature/better-exception
Create specific exception for each error
2 parents 8207227 + d94d556 commit a6bfe1b

File tree

7 files changed

+58
-21
lines changed

7 files changed

+58
-21
lines changed

src/Client.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Http\Client\Socket;
44

5-
use Http\Client\Exception\NetworkException;
65
use Http\Client\HttpClient;
6+
use Http\Client\Socket\Exception\ConnectionException;
7+
use Http\Client\Socket\Exception\InvalidRequestException;
8+
use Http\Client\Socket\Exception\SSLConnectionException;
79
use Http\Discovery\MessageFactoryDiscovery;
810
use Http\Message\ResponseFactory;
911
use Psr\Http\Message\RequestInterface;
@@ -98,7 +100,7 @@ public function sendRequest(RequestInterface $request)
98100
* @param string $remote Entrypoint for the connection
99101
* @param bool $useSsl Whether to use ssl or not
100102
*
101-
* @throws NetworkException When the connection fail
103+
* @throws ConnectionException|SSLConnectionException When the connection fail
102104
*
103105
* @return resource Socket resource
104106
*/
@@ -109,15 +111,13 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl)
109111
$socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']);
110112

111113
if (false === $socket) {
112-
throw new NetworkException($errMsg, $request);
114+
throw new ConnectionException($errMsg, $request);
113115
}
114116

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

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-
}
119+
if ($useSsl && false === @stream_socket_enable_crypto($socket, true, $this->config['ssl_method'])) {
120+
throw new SSLConnectionException(sprintf('Cannot enable tls: %s', error_get_last()['message']), $request);
121121
}
122122

123123
return $socket;
@@ -163,18 +163,18 @@ protected function configure(array $config = [])
163163
*
164164
* @param RequestInterface $request
165165
*
166-
* @throws NetworkException When no remote can be determined from the request
166+
* @throws InvalidRequestException When no remote can be determined from the request
167167
*
168168
* @return string
169169
*/
170170
private function determineRemoteFromRequest(RequestInterface $request)
171171
{
172-
if ($request->getUri()->getHost() == '' && !$request->hasHeader('Host')) {
173-
throw new NetworkException('Cannot find connection endpoint for this request', $request);
172+
if (!$request->hasHeader('Host') && $request->getUri()->getHost() === '') {
173+
throw new InvalidRequestException('Remote is not defined and we cannot determine a connection endpoint for this request (no Host header)', $request);
174174
}
175175

176176
$host = $request->getUri()->getHost();
177-
$port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() == 'https' ? 443 : 80);
177+
$port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() === 'https' ? 443 : 80);
178178
$endpoint = sprintf('%s:%s', $host, $port);
179179

180180
// 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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Http\Client\Socket;
44

5-
use Http\Client\Exception\NetworkException;
5+
use Http\Client\Socket\Exception\BrokenPipeException;
66
use Psr\Http\Message\RequestInterface;
77

88
/**
@@ -21,12 +21,12 @@ trait RequestWriter
2121
* @param RequestInterface $request
2222
* @param int $bufferSize
2323
*
24-
* @throws \Http\Client\Exception\NetworkException
24+
* @throws BrokenPipeException
2525
*/
2626
protected function writeRequest($socket, RequestInterface $request, $bufferSize = 8192)
2727
{
2828
if (false === $this->fwrite($socket, $this->transformRequestHeadersToString($request))) {
29-
throw new NetworkException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
29+
throw new BrokenPipeException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
3030
}
3131

3232
if ($request->getBody()->isReadable()) {
@@ -41,8 +41,7 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize
4141
* @param RequestInterface $request
4242
* @param int $bufferSize
4343
*
44-
* @throws \Http\Client\Exception\NetworkException
45-
* @throws \Http\Client\Exception\RequestException
44+
* @throws BrokenPipeException
4645
*/
4746
protected function writeBody($socket, RequestInterface $request, $bufferSize = 8192)
4847
{
@@ -56,7 +55,7 @@ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8
5655
$buffer = $body->read($bufferSize);
5756

5857
if (false === $this->fwrite($socket, $buffer)) {
59-
throw new NetworkException('An error occur when writing request to client (BROKEN EPIPE)', $request);
58+
throw new BrokenPipeException('An error occur when writing request to client (BROKEN EPIPE)', $request);
6059
}
6160
}
6261
}

src/ResponseReader.php

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

33
namespace Http\Client\Socket;
44

5-
use Http\Client\Exception\NetworkException;
5+
use Http\Client\Socket\Exception\BrokenPipeException;
6+
use Http\Client\Socket\Exception\TimeoutException;
67
use Http\Message\ResponseFactory;
78
use Psr\Http\Message\RequestInterface;
89
use Psr\Http\Message\ResponseInterface;
@@ -27,7 +28,8 @@ trait ResponseReader
2728
* @param RequestInterface $request
2829
* @param resource $socket
2930
*
30-
* @throws NetworkException When the response cannot be read
31+
* @throws TimeoutException When the socket timed out
32+
* @throws BrokenPipeException When the response cannot be read
3133
*
3234
* @return ResponseInterface
3335
*/
@@ -46,13 +48,13 @@ protected function readResponse(RequestInterface $request, $socket)
4648
$metadatas = stream_get_meta_data($socket);
4749

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

5254
$parts = explode(' ', array_shift($headers), 3);
5355

5456
if (count($parts) <= 1) {
55-
throw new NetworkException('Cannot read the response', $request);
57+
throw new BrokenPipeException('Cannot read the response', $request);
5658
}
5759

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

0 commit comments

Comments
 (0)