Skip to content
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

Replace custom ConnectionException with generic RuntimeException #72

Merged
merged 1 commit into from
Feb 8, 2017
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ contains a hostname, it will throw an `InvalidArgumentException`:
$server = new Server('127.0.0.1', $loop);
```

If the given URI appears to be valid, but listening on it fails (such as if port
is already in use or port below 1024 may require root access etc.), it will
throw a `RuntimeException`:

```php
$first = new Server(8080, $loop);

// throws RuntimeException because port is already in use
$second = new Server(8080, $loop);
```

> Note that these error conditions may vary depending on your system and/or
configuration.
See the exception message and code for more details about the actual error
condition.

Optionally, you can specify [socket context options](http://php.net/manual/en/context.socket.php)
for the underlying stream socket resource like this:

Expand Down
7 changes: 0 additions & 7 deletions src/ConnectionException.php

This file was deleted.

24 changes: 20 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use InvalidArgumentException;
use RuntimeException;

/**
* The `Server` class implements the `ServerInterface` and
Expand Down Expand Up @@ -84,6 +85,22 @@ class Server extends EventEmitter implements ServerInterface
* $server = new Server('127.0.0.1', $loop);
* ```
*
* If the given URI appears to be valid, but listening on it fails (such as if port
* is already in use or port below 1024 may require root access etc.), it will
* throw a `RuntimeException`:
*
* ```php
* $first = new Server(8080, $loop);
*
* // throws RuntimeException because port is already in use
* $second = new Server(8080, $loop);
* ```
*
* Note that these error conditions may vary depending on your system and/or
* configuration.
* See the exception message and code for more details about the actual error
* condition.
*
* Optionally, you can specify [socket context options](http://php.net/manual/en/context.socket.php)
* for the underlying stream socket resource like this:
*
Expand All @@ -104,7 +121,7 @@ class Server extends EventEmitter implements ServerInterface
* @param LoopInterface $loop
* @param array $context
* @throws InvalidArgumentException if the listening address is invalid
* @throws ConnectionException if listening on this address fails (already in use etc.)
* @throws RuntimeException if listening on this address fails (already in use etc.)
*/
public function __construct($uri, LoopInterface $loop, array $context = array())
{
Expand Down Expand Up @@ -136,7 +153,7 @@ public function __construct($uri, LoopInterface $loop, array $context = array())
}

if (false === filter_var(trim($parts['host'], '[]'), FILTER_VALIDATE_IP)) {
throw new \InvalidArgumentException('Given URI "' . $uri . '" does not contain a valid host IP');
throw new InvalidArgumentException('Given URI "' . $uri . '" does not contain a valid host IP');
}

$this->master = @stream_socket_server(
Expand All @@ -147,8 +164,7 @@ public function __construct($uri, LoopInterface $loop, array $context = array())
stream_context_create(array('socket' => $context))
);
if (false === $this->master) {
$message = "Could not bind to $uri: $errstr";
throw new ConnectionException($message, $errno);
throw new RuntimeException('Failed to listen on "' . $uri . '": ' . $errstr, $errno);
}
stream_set_blocking($this->master, 0);

Expand Down
7 changes: 3 additions & 4 deletions tests/FunctionalServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionException;
use React\Socket\ConnectionInterface;
use React\Socket\ServerInterface;
use React\SocketClient\TcpConnector;
Expand Down Expand Up @@ -161,7 +160,7 @@ public function testEmitsConnectionForNewIpv6Connection()

try {
$server = new Server('[::1]:0', $loop);
} catch (ConnectionException $e) {
} catch (\RuntimeException $e) {
$this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)');
}

Expand All @@ -182,7 +181,7 @@ public function testEmitsConnectionWithRemoteIpv6()

try {
$server = new Server('[::1]:0', $loop);
} catch (ConnectionException $e) {
} catch (\RuntimeException $e) {
$this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)');
}

Expand All @@ -208,7 +207,7 @@ public function testEmitsConnectionWithLocalIpv6()

try {
$server = new Server('[::1]:0', $loop);
} catch (ConnectionException $e) {
} catch (\RuntimeException $e) {
$this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)');
}

Expand Down
6 changes: 5 additions & 1 deletion tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,14 @@ public function testConnectionDoesEndWhenClientCloses()
}

/**
* @expectedException React\Socket\ConnectionException
* @expectedException RuntimeException
*/
public function testListenOnBusyPortThrows()
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Windows supports listening on same port multiple times');
}

$another = new Server($this->port, $this->loop);
}

Expand Down