more exception handling cleanup

This commit is contained in:
Dylan K. Taylor 2019-01-22 13:37:46 +00:00
parent 053defb7dc
commit 48a99937b9
3 changed files with 33 additions and 13 deletions

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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.");
}
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;
}
try{
/** @noinspection PhpUndefinedClassInspection */
$com = new \COM("HNetCfg.NATUPnP");
/** @noinspection PhpUndefinedFieldInspection */
if($com === false or !is_object($com->StaticPortMappingCollection)){
return false;
}
try{
/** @noinspection PhpUndefinedFieldInspection */
$com->StaticPortMappingCollection->Remove($port, "UDP");
}catch(\Throwable $e){
}catch(\com_exception $e){
//TODO: should this really be silenced?
return false;
}