improved error / quit messages

This commit is contained in:
Steffen Schröder 2014-05-09 14:08:39 +02:00
parent 36531b9227
commit 991b9048ba
2 changed files with 62 additions and 57 deletions

View File

@ -34,22 +34,22 @@ class Database implements TimerListener {
$host = $this->maniaControl->config->database->xpath('host'); $host = $this->maniaControl->config->database->xpath('host');
if (!$host) { if (!$host) {
$message = "Invalid database configuration (host)."; $message = "Invalid database configuration (host).";
$this->maniaControl->quit($message); $this->maniaControl->quit($message, true);
} }
$port = $this->maniaControl->config->database->xpath('port'); $port = $this->maniaControl->config->database->xpath('port');
if (!$port) { if (!$port) {
$message = "Invalid database configuration (port)."; $message = "Invalid database configuration (port).";
$this->maniaControl->quit($message); $this->maniaControl->quit($message, true);
} }
$user = $this->maniaControl->config->database->xpath('user'); $user = $this->maniaControl->config->database->xpath('user');
if (!$user) { if (!$user) {
$message = "Invalid database configuration (user)."; $message = "Invalid database configuration (user).";
$this->maniaControl->quit($message); $this->maniaControl->quit($message, true);
} }
$pass = $this->maniaControl->config->database->xpath('pass'); $pass = $this->maniaControl->config->database->xpath('pass');
if (!$pass) { if (!$pass) {
$message = "Invalid database configuration (pass)."; $message = "Invalid database configuration (pass).";
$this->maniaControl->quit($message); $this->maniaControl->quit($message, true);
} }
$host = (string)$host[0]; $host = (string)$host[0];
@ -64,7 +64,7 @@ class Database implements TimerListener {
$this->mysqli = @new \mysqli($host, $user, $pass, null, $port); $this->mysqli = @new \mysqli($host, $user, $pass, null, $port);
if ($this->mysqli->connect_error) { if ($this->mysqli->connect_error) {
$message = "Couldn't connect to Database: '{$this->mysqli->connect_error}'"; $message = "Couldn't connect to Database: '{$this->mysqli->connect_error}'";
$this->maniaControl->quit($message); $this->maniaControl->quit($message, true);
} }
$this->mysqli->set_charset("utf8"); $this->mysqli->set_charset("utf8");
@ -86,7 +86,7 @@ class Database implements TimerListener {
private function initDatabase() { private function initDatabase() {
$dbName = $this->maniaControl->config->database->xpath('db_name'); $dbName = $this->maniaControl->config->database->xpath('db_name');
if (!$dbName) { if (!$dbName) {
trigger_error("Invalid database configuration (database).", E_USER_ERROR); $this->maniaControl->quit("Invalid Database Configuration (db_name).", true);
return false; return false;
} }
$dbName = (string)$dbName[0]; $dbName = (string)$dbName[0];
@ -101,13 +101,13 @@ class Database implements TimerListener {
$databaseQuery = "CREATE DATABASE ?;"; $databaseQuery = "CREATE DATABASE ?;";
$databaseStatement = $this->mysqli->prepare($databaseQuery); $databaseStatement = $this->mysqli->prepare($databaseQuery);
if ($this->mysqli->error) { if ($this->mysqli->error) {
trigger_error($this->mysqli->error, E_USER_ERROR); $this->maniaControl->quit($this->mysqli->error, true);
return false; return false;
} }
$databaseStatement->bind_param('s', $dbName); $databaseStatement->bind_param('s', $dbName);
$databaseStatement->execute(); $databaseStatement->execute();
if ($databaseStatement->error) { if ($databaseStatement->error) {
trigger_error($databaseStatement->error, E_USER_ERROR); $this->maniaControl->quit($databaseStatement->error, true);
return false; return false;
} }
$databaseStatement->close(); $databaseStatement->close();
@ -115,7 +115,8 @@ class Database implements TimerListener {
// Connect to new database // Connect to new database
$this->mysqli->select_db($dbName); $this->mysqli->select_db($dbName);
if ($this->mysqli->error) { if ($this->mysqli->error) {
trigger_error("Couldn't select database '{$dbName}'. " . $this->mysqli->error, E_USER_ERROR); $message = "Couldn't select database '{$dbName}'. {$this->mysqli->error}";
$this->maniaControl->quit($message, true);
return false; return false;
} }
return true; return true;
@ -161,11 +162,11 @@ class Database implements TimerListener {
/** /**
* Check if Connection still exists every 5 seconds * Check if Connection still exists every 5 seconds
* *
* @param $time * @param float $time
*/ */
public function checkConnection($time) { public function checkConnection($time = null) {
if (!$this->mysqli->ping()) { if (!$this->mysqli || !$this->mysqli->ping()) {
$this->maniaControl->quit("The MySQL server has gone away!"); $this->maniaControl->quit("The MySQL server has gone away!", true);
} }
} }

View File

@ -271,9 +271,13 @@ class ManiaControl implements CommandListener, TimerListener {
* Quit ManiaControl and log the given message * Quit ManiaControl and log the given message
* *
* @param string $message * @param string $message
* @param bool $errorPrefix
*/ */
public function quit($message = null) { public function quit($message = null, $errorPrefix = false) {
if ($message) { if ($message) {
if ($errorPrefix) {
$message = '[ERROR] ' . $message;
}
$this->log($message); $this->log($message);
} }
exit(); exit();
@ -365,6 +369,46 @@ class ManiaControl implements CommandListener, TimerListener {
$this->quit(); $this->quit();
} }
/**
* Connect to ManiaPlanet server
*/
private function connect() {
// Load remote client
$this->server->loadConfig();
$this->log("Connecting to server at {$this->server->config->host}:{$this->server->config->port}...");
try {
$this->client = Connection::factory($this->server->config->host, $this->server->config->port, self::CONNECT_TIMEOUT, $this->server->config->login, $this->server->config->pass);
} catch (Exception $e) {
$message = "Couldn't authenticate on Server with User '{$this->server->config->login}' & Pass '{$this->server->config->pass}'! " . $e->getMessage();
$this->quit($message, true);
}
// Enable callback system
$this->client->enableCallbacks(true);
// Wait for server to be ready
try {
if (!$this->server->waitForStatus(4)) {
$this->quit("Server couldn't get ready!");
}
} catch (Exception $e) {
// TODO remove
$this->errorHandler->handleException($e, false);
$this->quit($e->getMessage(), true);
}
// Connect finished
$this->log("Server Connection successfully established!");
// Hide old widgets
$this->client->sendHideManialinkPage();
// Enable script callbacks
$this->server->scriptManager->enableScriptCallbacks();
}
/** /**
* Perform the Main Loop * Perform the Main Loop
*/ */
@ -380,7 +424,7 @@ class ManiaControl implements CommandListener, TimerListener {
$this->log("Connection interrupted!"); $this->log("Connection interrupted!");
// TODO remove // TODO remove
$this->errorHandler->handleException($e, false); $this->errorHandler->handleException($e, false);
$this->quit($e->getMessage()); $this->quit($e->getMessage(), true);
} }
// Manage FileReader // Manage FileReader
@ -394,44 +438,4 @@ class ManiaControl implements CommandListener, TimerListener {
usleep($sleepTime); usleep($sleepTime);
} }
} }
/**
* Connect to ManiaPlanet server
*/
private function connect() {
// Load remote client
$this->server->loadConfig();
$this->log("Connecting to server at {$this->server->config->host}:{$this->server->config->port}...");
try {
$this->client = Connection::factory($this->server->config->host, $this->server->config->port, self::CONNECT_TIMEOUT, $this->server->config->login, $this->server->config->pass);
} catch (Exception $e) {
$message = "Couldn't authenticate on Server with User '{$this->server->config->login}' & Pass '{$this->server->config->pass}'! " . $e->getMessage();
$this->quit($message);
}
// Enable callback system
$this->client->enableCallbacks(true);
// Wait for server to be ready
try {
if (!$this->server->waitForStatus(4)) {
$this->quit("Server couldn't get ready!");
}
} catch (Exception $e) {
// TODO remove
$this->errorHandler->handleException($e, false);
$this->quit($e->getMessage());
}
// Connect finished
$this->log("Server Connection successfully established!");
// Hide old widgets
$this->client->sendHideManialinkPage();
// Enable script callbacks
$this->server->scriptManager->enableScriptCallbacks();
}
} }