From 48a99937b9556d0b04121d58ea45dce994d88633 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 22 Jan 2019 13:37:46 +0000 Subject: [PATCH] more exception handling cleanup --- src/pocketmine/Server.php | 6 +++--- src/pocketmine/network/rcon/RCON.php | 11 ++++++++++- src/pocketmine/network/upnp/UPnP.php | 29 +++++++++++++++++++--------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 235b276bfc..d8d6ddc366 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -701,7 +701,7 @@ class Server{ $nbt = new BigEndianNbtSerializer(); try{ file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed($ev->getSaveData())); - }catch(\Throwable $e){ + }catch(\ErrorException $e){ $this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()])); $this->logger->logException($e); } @@ -1200,7 +1200,7 @@ class Server{ $this->getIp(), $this->getConfigInt("rcon.max-clients", 50) ); - }catch(\Exception $e){ + }catch(\RuntimeException $e){ $this->getLogger()->critical("RCON can't be started: " . $e->getMessage()); } } @@ -1782,7 +1782,7 @@ class Server{ $this->logger->info("[UPnP] Trying to port forward..."); try{ UPnP::PortForward($this->getPort()); - }catch(\Exception $e){ + }catch(\RuntimeException $e){ $this->logger->alert("UPnP portforward failed: " . $e->getMessage()); } } diff --git a/src/pocketmine/network/rcon/RCON.php b/src/pocketmine/network/rcon/RCON.php index ffcf1214b5..09aa4be249 100644 --- a/src/pocketmine/network/rcon/RCON.php +++ b/src/pocketmine/network/rcon/RCON.php @@ -64,11 +64,20 @@ class RCON{ /** @var resource */ private $ipcThreadSocket; + /** + * @param Server $server + * @param string $password + * @param int $port + * @param string $interface + * @param int $maxClients + * + * @throws \RuntimeException + */ public function __construct(Server $server, string $password, int $port = 19132, string $interface = "0.0.0.0", int $maxClients = 50){ $this->server = $server; $this->server->getLogger()->info("Starting remote control listener"); if($password === ""){ - throw new \InvalidArgumentException("Empty password"); + throw new \RuntimeException("Empty password"); } $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); diff --git a/src/pocketmine/network/upnp/UPnP.php b/src/pocketmine/network/upnp/UPnP.php index aa9c3a7334..35b19a0337 100644 --- a/src/pocketmine/network/upnp/UPnP.php +++ b/src/pocketmine/network/upnp/UPnP.php @@ -35,6 +35,11 @@ use function trim; abstract class UPnP{ + /** + * @param int $port + * + * @throws \RuntimeException + */ public static function PortForward(int $port) : void{ if(!Internet::$online){ throw new \RuntimeException("Server is offline"); @@ -56,8 +61,12 @@ abstract class UPnP{ throw new \RuntimeException("Failed to portforward using UPnP. Ensure that network discovery is enabled in Control Panel."); } - /** @noinspection PhpUndefinedFieldInspection */ - $com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP"); + try{ + /** @noinspection PhpUndefinedFieldInspection */ + $com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP"); + }catch(\com_exception $e){ + throw new \RuntimeException($e->getMessage(), 0, $e); + } } public static function RemovePortForward(int $port) : bool{ @@ -68,16 +77,18 @@ abstract class UPnP{ return false; } + /** @noinspection PhpUndefinedClassInspection */ + $com = new \COM("HNetCfg.NATUPnP"); + /** @noinspection PhpUndefinedFieldInspection */ + if($com === false or !is_object($com->StaticPortMappingCollection)){ + return false; + } + try{ - /** @noinspection PhpUndefinedClassInspection */ - $com = new \COM("HNetCfg.NATUPnP"); - /** @noinspection PhpUndefinedFieldInspection */ - if($com === false or !is_object($com->StaticPortMappingCollection)){ - return false; - } /** @noinspection PhpUndefinedFieldInspection */ $com->StaticPortMappingCollection->Remove($port, "UDP"); - }catch(\Throwable $e){ + }catch(\com_exception $e){ + //TODO: should this really be silenced? return false; }