Skip to content

Commit

Permalink
Merge pull request #774 from simPod/exc
Browse files Browse the repository at this point in the history
Use Throwable instead of Exception
  • Loading branch information
cboden authored Jun 4, 2020
2 parents 0469b63 + 3931437 commit 04b4599
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* 0.5.0
* PHP 7.2 is minimum supported version
* BC: `Ratchet\Session\Storage\Proxy\VirtualSessionStorage` requires `Ratchet\Session\OptionsHandler`
* `ComponentInterface::onError()` now accepts `Throwable` instead of `Exception`

* 0.4.1 (2017-12-11)
* Only enableKeepAlive in App if no WsServer passed allowing user to set their own timeout duration
Expand Down
16 changes: 9 additions & 7 deletions src/Ratchet/ComponentInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Ratchet;

use Throwable;

/**
* This is the interface to build a Ratchet application with.
* It implements the decorator pattern to build an application stack
Expand All @@ -9,23 +11,23 @@ interface ComponentInterface {
/**
* When a new connection is opened it will be passed to this method
* @param ConnectionInterface $conn The socket/connection that just connected to your application
* @throws \Exception
* @throws Throwable
*/
function onOpen(ConnectionInterface $conn);

/**
* This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed.
* @param ConnectionInterface $conn The socket/connection that is closing/closed
* @throws \Exception
* @throws Throwable
*/
function onClose(ConnectionInterface $conn);

/**
* If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
* the Exception is sent back down the stack, handled by the Server and bubbled back up the application through this method
* If there is an error with one of the sockets, or somewhere in the application where an Throwable is thrown,
* the Throwable is sent back down the stack, handled by the Server and bubbled back up the application through this method
* @param ConnectionInterface $conn
* @param \Exception $e
* @throws \Exception
* @param Throwable $e
* @throws Throwable
*/
function onError(ConnectionInterface $conn, \Exception $e);
function onError(ConnectionInterface $conn, Throwable $e);
}
3 changes: 2 additions & 1 deletion src/Ratchet/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ratchet\Http;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Throwable;

class HttpServer implements MessageComponentInterface {
use CloseResponseTrait;
Expand Down Expand Up @@ -66,7 +67,7 @@ public function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
if ($conn->httpHeadersReceived) {
$this->_httpServer->onError($conn, $e);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/Ratchet/Http/NoOpHttpServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ratchet\Http;
use Ratchet\ConnectionInterface;
use Psr\Http\Message\RequestInterface;
use Throwable;

class NoOpHttpServerController implements HttpServerInterface {
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
Expand All @@ -13,6 +14,6 @@ public function onMessage(ConnectionInterface $from, $msg) {
public function onClose(ConnectionInterface $conn) {
}

public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
}
}
5 changes: 3 additions & 2 deletions src/Ratchet/Http/OriginCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Psr\Http\Message\RequestInterface;
use Throwable;

/**
* A middleware to ensure JavaScript clients connecting are from the expected domain.
Expand Down Expand Up @@ -59,7 +60,7 @@ function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
function onError(ConnectionInterface $conn, \Exception $e) {
function onError(ConnectionInterface $conn, Throwable $e) {
return $this->_component->onError($conn, $e);
}
}
}
3 changes: 2 additions & 1 deletion src/Ratchet/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use GuzzleHttp\Psr7 as gPsr;
use Throwable;

class Router implements HttpServerInterface {
use CloseResponseTrait;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
if (isset($conn->controller)) {
$conn->controller->onError($conn, $e);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Ratchet/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php
namespace Ratchet;

use Throwable;

interface MessageInterface {
/**
* Triggered when a client sends data through the socket
* @param \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
* @param string $msg The message received
* @throws \Exception
* @throws Throwable
*/
function onMessage(ConnectionInterface $from, $msg);
}
3 changes: 2 additions & 1 deletion src/Ratchet/Server/EchoServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ratchet\Server;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Throwable;

/**
* A simple Ratchet application that will reply to all messages with the message it received
Expand All @@ -17,7 +18,7 @@ public function onMessage(ConnectionInterface $from, $msg) {
public function onClose(ConnectionInterface $conn) {
}

public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
$conn->close();
}
}
3 changes: 2 additions & 1 deletion src/Ratchet/Server/FlashPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ratchet\Server;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Throwable;

/**
* An app to go on a server stack to pass a policy file to a Flash socket
Expand Down Expand Up @@ -132,7 +133,7 @@ public function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
$conn->close();
}

Expand Down
11 changes: 6 additions & 5 deletions src/Ratchet/Server/IoServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use React\EventLoop\Factory as LoopFactory;
use React\Socket\Server as Reactor;
use React\Socket\SecureServer as SecureReactor;
use Throwable;

/**
* Creates an open-ended socket to listen on a port for incoming connections.
Expand Down Expand Up @@ -97,7 +98,7 @@ public function handleConnect($conn) {
$conn->on('close', function () use ($conn) {
$this->handleEnd($conn);
});
$conn->on('error', function (\Exception $e) use ($conn) {
$conn->on('error', function (Throwable $e) use ($conn) {
$this->handleError($e, $conn);
});
}
Expand All @@ -110,7 +111,7 @@ public function handleConnect($conn) {
public function handleData($data, $conn) {
try {
$this->app->onMessage($conn->decor, $data);
} catch (\Exception $e) {
} catch (Throwable $e) {
$this->handleError($e, $conn);
}
}
Expand All @@ -122,7 +123,7 @@ public function handleData($data, $conn) {
public function handleEnd($conn) {
try {
$this->app->onClose($conn->decor);
} catch (\Exception $e) {
} catch (Throwable $e) {
$this->handleError($e, $conn);
}

Expand All @@ -131,10 +132,10 @@ public function handleEnd($conn) {

/**
* An error has occurred, let the listening application know
* @param \Exception $e
* @param Throwable $e
* @param \React\Socket\ConnectionInterface $conn
*/
public function handleError(\Exception $e, $conn) {
public function handleError(Throwable $e, $conn) {
$this->app->onError($conn->decor, $e);
}
}
5 changes: 3 additions & 2 deletions src/Ratchet/Server/IpBlackList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ratchet\Server;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Throwable;

class IpBlackList implements MessageComponentInterface {
/**
Expand Down Expand Up @@ -67,7 +68,7 @@ public function getBlockedAddresses() {
*/
public function filterAddress($address) {
if (strstr($address, ':') && substr_count($address, '.') == 3) {
list($address, $port) = explode(':', $address);
[$address, $port] = explode(':', $address);
}

return $address;
Expand Down Expand Up @@ -103,7 +104,7 @@ function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
function onError(ConnectionInterface $conn, \Exception $e) {
function onError(ConnectionInterface $conn, Throwable $e) {
if (!$this->isBlocked($conn->remoteAddress)) {
$this->_decorating->onError($conn, $e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Ratchet/Session/SessionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Ratchet\Session\Serialize\HandlerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Throwable;

/**
* This component will allow access to session data from your website for each user connected
Expand Down Expand Up @@ -125,7 +126,7 @@ function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
function onError(ConnectionInterface $conn, \Exception $e) {
function onError(ConnectionInterface $conn, Throwable $e) {
return $this->_app->onError($conn, $e);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Ratchet/Wamp/ServerProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\ConnectionInterface;
use Throwable;

/**
* WebSocket Application Messaging Protocol
Expand Down Expand Up @@ -155,7 +156,7 @@ public function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
return $this->_decorating->onError($this->connections[$conn], $e);
}
}
3 changes: 2 additions & 1 deletion src/Ratchet/Wamp/TopicManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ratchet\Wamp;
use Ratchet\ConnectionInterface;
use Ratchet\WebSocket\WsServerInterface;
use Throwable;

class TopicManager implements WsServerInterface, WampServerInterface {
/**
Expand Down Expand Up @@ -84,7 +85,7 @@ public function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
$this->app->onError($conn, $e);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Ratchet/Wamp/WampServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\ConnectionInterface;
use Throwable;

/**
* Enable support for the official WAMP sub-protocol in your application
Expand Down Expand Up @@ -39,7 +40,7 @@ public function onOpen(ConnectionInterface $conn) {
public function onMessage(ConnectionInterface $conn, $msg) {
try {
$this->wampProtocol->onMessage($conn, $msg);
} catch (Exception $we) {
} catch (Throwable $we) {
$conn->close(1007);
}
}
Expand All @@ -54,7 +55,7 @@ public function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
$this->wampProtocol->onError($conn, $e);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Ratchet/WebSocket/WsServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Ratchet\RFC6455\Handshake\RequestVerifier;
use React\EventLoop\LoopInterface;
use GuzzleHttp\Psr7 as gPsr;
use Throwable;

/**
* The adapter to handle WebSocket requests/responses
Expand Down Expand Up @@ -168,7 +169,7 @@ public function onClose(ConnectionInterface $conn) {
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
if ($this->connections->contains($conn)) {
$this->delegate->onError($this->connections[$conn]->connection, $e);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/autobahn/bin/fuzzingserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function onOpen(ConnectionInterface $conn) {
public function onClose(ConnectionInterface $conn) {
}

public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/helpers/Ratchet/Mock/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\ConnectionInterface;
use Throwable;

class Component implements MessageComponentInterface, WsServerInterface {
public $last = array();
Expand All @@ -25,7 +26,7 @@ public function onClose(ConnectionInterface $conn) {
$this->last[__FUNCTION__] = func_get_args();
}

public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
$this->last[__FUNCTION__] = func_get_args();
}

Expand Down
3 changes: 2 additions & 1 deletion tests/helpers/Ratchet/Mock/WampComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Ratchet\Wamp\WampServerInterface;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\ConnectionInterface;
use Throwable;

class WampComponent implements WampServerInterface, WsServerInterface {
public $last = array();
Expand Down Expand Up @@ -37,7 +38,7 @@ public function onClose(ConnectionInterface $conn) {
$this->last[__FUNCTION__] = func_get_args();
}

public function onError(ConnectionInterface $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, Throwable $e) {
$this->last[__FUNCTION__] = func_get_args();
}
}
5 changes: 2 additions & 3 deletions tests/helpers/Ratchet/NullComponent.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
namespace Ratchet;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\Wamp\WampServerInterface;
use Throwable;

class NullComponent implements MessageComponentInterface, WsServerInterface, WampServerInterface {
public function onOpen(ConnectionInterface $conn) {}
Expand All @@ -12,7 +11,7 @@ public function onMessage(ConnectionInterface $conn, $msg) {}

public function onClose(ConnectionInterface $conn) {}

public function onError(ConnectionInterface $conn, \Exception $e) {}
public function onError(ConnectionInterface $conn, Throwable $e) {}

public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {}

Expand Down

0 comments on commit 04b4599

Please sign in to comment.