Skip to content

Create specific exception for each error #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Http\Client\Socket;

use Http\Client\Exception\NetworkException;
use Http\Client\HttpClient;
use Http\Client\Socket\Exception\ConnectionException;
use Http\Client\Socket\Exception\InvalidRequestException;
use Http\Client\Socket\Exception\SSLConnectionException;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\ResponseFactory;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -98,7 +100,7 @@ public function sendRequest(RequestInterface $request)
* @param string $remote Entrypoint for the connection
* @param bool $useSsl Whether to use ssl or not
*
* @throws NetworkException When the connection fail
* @throws ConnectionException|SSLConnectionException When the connection fail
*
* @return resource Socket resource
*/
Expand All @@ -109,15 +111,13 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl)
$socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']);

if (false === $socket) {
throw new NetworkException($errMsg, $request);
throw new ConnectionException($errMsg, $request);
}

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

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

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

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

// If use the host header if present for the endpoint
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/BrokenPipeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class BrokenPipeException extends NetworkException
{
}
9 changes: 9 additions & 0 deletions src/Exception/ConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class ConnectionException extends NetworkException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class InvalidRequestException extends NetworkException
{
}
9 changes: 9 additions & 0 deletions src/Exception/SSLConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class SSLConnectionException extends NetworkException
{
}
11 changes: 5 additions & 6 deletions src/RequestWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Http\Client\Socket;

use Http\Client\Exception\NetworkException;
use Http\Client\Socket\Exception\BrokenPipeException;
use Psr\Http\Message\RequestInterface;

/**
Expand All @@ -21,12 +21,12 @@ trait RequestWriter
* @param RequestInterface $request
* @param int $bufferSize
*
* @throws \Http\Client\Exception\NetworkException
* @throws BrokenPipeException
*/
protected function writeRequest($socket, RequestInterface $request, $bufferSize = 8192)
{
if (false === $this->fwrite($socket, $this->transformRequestHeadersToString($request))) {
throw new NetworkException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
throw new BrokenPipeException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
}

if ($request->getBody()->isReadable()) {
Expand All @@ -41,8 +41,7 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize
* @param RequestInterface $request
* @param int $bufferSize
*
* @throws \Http\Client\Exception\NetworkException
* @throws \Http\Client\Exception\RequestException
* @throws BrokenPipeException
*/
protected function writeBody($socket, RequestInterface $request, $bufferSize = 8192)
{
Expand All @@ -56,7 +55,7 @@ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8
$buffer = $body->read($bufferSize);

if (false === $this->fwrite($socket, $buffer)) {
throw new NetworkException('An error occur when writing request to client (BROKEN EPIPE)', $request);
throw new BrokenPipeException('An error occur when writing request to client (BROKEN EPIPE)', $request);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/ResponseReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Http\Client\Socket;

use Http\Client\Exception\NetworkException;
use Http\Client\Socket\Exception\BrokenPipeException;
use Http\Client\Socket\Exception\TimeoutException;
use Http\Message\ResponseFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -27,7 +28,8 @@ trait ResponseReader
* @param RequestInterface $request
* @param resource $socket
*
* @throws NetworkException When the response cannot be read
* @throws TimeoutException When the socket timed out
* @throws BrokenPipeException When the response cannot be read
*
* @return ResponseInterface
*/
Expand All @@ -46,13 +48,13 @@ protected function readResponse(RequestInterface $request, $socket)
$metadatas = stream_get_meta_data($socket);

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

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

if (count($parts) <= 1) {
throw new NetworkException('Cannot read the response', $request);
throw new BrokenPipeException('Cannot read the response', $request);
}

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