Browse Source

Improvements in handling exceptions, database now showing all errors through exceptions.

ohartl 9 năm trước cách đây
mục cha
commit
410fe43330

+ 32 - 39
include/php/classes/Database.php

@@ -27,15 +27,26 @@ class Database
 	protected $lastQuery;
 	protected $lastQuery;
 
 
 
 
+	/**
+	 * @param string $host
+	 * @param string $user
+	 * @param string $password
+	 * @param string $database
+	 *
+	 * @throws Exception
+	 */
 	protected function __construct($host, $user, $password, $database)
 	protected function __construct($host, $user, $password, $database)
 	{
 	{
 		if(!static::isInitialized()){
 		if(!static::isInitialized()){
 			$this->config = $database;
 			$this->config = $database;
 
 
-			$this->db = new mysqli($host, $user, $password, $database);
-			if($this->db->connect_errno > 0){
-				$this->db = null;
-				die('Unable to connect to database ['.$this->db->connect_error.']');
+			try{
+				mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
+
+				$this->db = new mysqli($host, $user, $password, $database);
+			}
+			catch(mysqli_sql_exception $e){
+				throw new Exception('Unable to connect to database', 0, $e);
 			}
 			}
 		}
 		}
 	}
 	}
@@ -48,9 +59,15 @@ class Database
 
 
 	/**
 	/**
 	 * @return Database
 	 * @return Database
+	 *
+	 * @throws Exception
 	 */
 	 */
 	public static function getInstance()
 	public static function getInstance()
 	{
 	{
+		if(!static::isInitialized()){
+			throw new Exception('Database must be initialized before using it (see Database::init).');
+		}
+
 		return static::$instance;
 		return static::$instance;
 	}
 	}
 
 
@@ -105,46 +122,14 @@ class Database
 	}
 	}
 
 
 
 
-	/**
-	 *
-	 */
-	public static function mustBeInitialized()
-	{
-		if(!static::isInitialized()){
-			die('Database has not been initialized.');
-		}
-	}
-
-
-	/**
-	 * Die with error and executed sql query
-	 *
-	 * @param string $errorMessage
-	 * @param string|null $sql
-	 */
-	public function dieOnDatabaseError($errorMessage, $sql = null)
-	{
-		die('There was an error running the query ['.$errorMessage.']'.(!is_null($sql) ? ' with statement "'.$sql.'"' : ''));
-	}
-
-
-	/**
-	 * Die if query not successful
-	 */
-	public function mustBeSuccessful()
-	{
-		if($this->db->errno !== 0){
-			$this->dieOnDatabaseError($this->db->error, $this->lastQuery);
-		}
-	}
-
-
 	/**
 	/**
 	 * Execute query
 	 * Execute query
 	 *
 	 *
 	 * @param string $query
 	 * @param string $query
 	 *
 	 *
 	 * @return bool|mysqli_result
 	 * @return bool|mysqli_result
+	 *
+	 * @throws DatabaseException
 	 */
 	 */
 	public function query($query)
 	public function query($query)
 	{
 	{
@@ -152,7 +137,15 @@ class Database
 
 
 		$result = $this->db->query($query);
 		$result = $this->db->query($query);
 
 
-		$this->mustBeSuccessful();
+		if($this->db->errno !== 0){
+			$ex = new DatabaseException('There was an error running the query ['.$this->db->error.']');
+
+			if(!is_null($this->lastQuery)){
+				$ex->setQuery($this->lastQuery);
+			}
+
+			throw $ex;
+		}
 
 
 		return $result;
 		return $result;
 	}
 	}

+ 31 - 0
include/php/classes/DatabaseException.php

@@ -0,0 +1,31 @@
+<?php
+
+class DatabaseException extends Exception
+{
+	/** @var string */
+	protected $query;
+
+	/**
+	 * Set the executed SQL query
+	 *
+	 * @param string $query
+	 *
+	 * @return $this
+	 */
+	public function setQuery($query)
+	{
+		$this->query = $query;
+
+		return $this;
+	}
+
+	/**
+	 * Get the executed SQL query
+	 *
+	 * @return string
+	 */
+	public function getQuery()
+	{
+		return $this->query;
+	}
+}

+ 13 - 5
include/php/global.inc.php

@@ -5,18 +5,26 @@
  * Add message to logfile
  * Add message to logfile
  *
  *
  * @param string $text
  * @param string $text
+ *
+ * @throws Exception
  */
  */
 function writeLog($text)
 function writeLog($text)
 {
 {
 	if(Config::get('options.enable_logging', false) && Config::has('log_path')){
 	if(Config::get('options.enable_logging', false) && Config::has('log_path')){
-		$logdestination = realpath(Config::get('log_path')).DIRECTORY_SEPARATOR."webmum.log";
+
+		$logDestination = realpath(Config::get('log_path')).DIRECTORY_SEPARATOR."webmum.log";
+
 		if(is_writable(Config::get('log_path'))){
 		if(is_writable(Config::get('log_path'))){
-			$logfile = fopen($logdestination, "a") or die("Unable to create or open logfile \"".$logdestination."\" in root directory!");
-			fwrite($logfile, date('M d H:i:s').": ".$text."\n");
-			fclose($logfile);
+			if($logfile = fopen($logDestination, "a")){
+				fwrite($logfile, date('M d H:i:s').": ".$text."\n");
+				fclose($logfile);
+			}
+			else{
+				throw new Exception('Unable to create or open logfile "'.$logDestination.'" in root directory!');
+			}
 		}
 		}
 		else{
 		else{
-			die("Directory \"".Config::get('log_path')."\" isn't writable");
+			throw new Exception('Directory "'.Config::get('log_path').'" isn\'t writable');
 		}
 		}
 	}
 	}
 }
 }

+ 17 - 9
index.php

@@ -1,15 +1,23 @@
 <?php
 <?php
 
 
-/**
- * Loading system
- */
-require_once 'include/php/default.inc.php';
+try {
+	/**
+	 * Loading system
+	 */
+	require_once 'include/php/default.inc.php';
 
 
-
-/**
- * Handle request
- */
-$content = Router::executeCurrentRequest();
+	
+	/**
+	 * Handle request
+	 */
+	$content = Router::executeCurrentRequest();
+}
+catch(DatabaseException $e){
+	$content = '<div class="notification notification-fail">Faulty database query: "'.$e->getQuery().'".</div>';
+}
+catch(Exception $e){
+	$content = '<div class="notification notification-fail">'.$e->getMessage().'</div>';
+}
 
 
 if(defined('USING_OLD_CONFIG')){
 if(defined('USING_OLD_CONFIG')){
 	$content = '<div class="notification notification-fail"><strong>Your WebMUM installation is still using the old deprecated config style!</strong><br><br>Please update your config to the new style (an example config can be found in <cite>config.php.example</cite>)<br>and delete your old <cite>config.inc.php</cite> and <cite>config.inc.php.example</cite>.</div>'.$content;
 	$content = '<div class="notification notification-fail"><strong>Your WebMUM installation is still using the old deprecated config style!</strong><br><br>Please update your config to the new style (an example config can be found in <cite>config.php.example</cite>)<br>and delete your old <cite>config.inc.php</cite> and <cite>config.inc.php.example</cite>.</div>'.$content;