Browse Source

reinstate error handlers; better DB error reporting on failed queries

Andrew Dolgov 7 years ago
parent
commit
aca75cb5cb
6 changed files with 17 additions and 20 deletions
  1. 1 6
      classes/db.php
  2. 2 4
      classes/db/mysql.php
  3. 2 3
      classes/db/pgsql.php
  4. 0 1
      classes/idb.php
  5. 2 1
      classes/logger/sql.php
  6. 10 5
      include/errorhandler.php

+ 1 - 6
classes/db.php

@@ -13,11 +13,10 @@ class Db implements IDb {
 			$this->adapter = new Db_Pgsql();
 			break;
 		default:
-			user_error("Unknown DB_TYPE: " . DB_TYPE);
+			die("Unknown DB_TYPE: " . DB_TYPE);
 		}
 
 		$this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
-
 	}
 
 	private function __clone() {
@@ -35,10 +34,6 @@ class Db implements IDb {
 		return("'$str'");
 	}
 
-	function init() {
-		//
-	}
-
 	function connect($host, $user, $pass, $db, $port) {
 		//return $this->adapter->connect($host, $user, $pass, $db, $port);
 		return $this->link;

+ 2 - 4
classes/db/mysql.php

@@ -25,10 +25,8 @@ class Db_Mysql implements IDb {
 	function query($query, $die_on_error = true) {
 		$result = mysql_query($query, $this->link);
 		if (!$result) {
-			$query = htmlspecialchars($query);
-			if ($die_on_error) {
-				die("Query <i>$query</i> failed: " . ($this->link ? mysql_error($link) : "No connection"));
-			}
+			user_error("Query $query failed: " . ($this->link ? mysql_error($this->link) : "No connection"),
+				$die_on_error ? E_USER_ERROR : E_USER_WARNING);
 		}
 		return $result;
 	}

+ 2 - 3
classes/db/pgsql.php

@@ -39,9 +39,8 @@ class Db_Pgsql implements IDb {
 
 		if (!$result) {
 			$query = htmlspecialchars($query); // just in case
-			if ($die_on_error) {
-				die("Query <i>$query</i> failed [$result]: " . ($this->link ? pg_last_error($this->link) : "No connection"));
-			}
+			user_error("Query $query failed: " . ($this->link ? pg_last_error($this->link) : "No connection"),
+				$die_on_error ? E_USER_ERROR : E_USER_WARNING);
 		}
 		return $result;
 	}

+ 0 - 1
classes/idb.php

@@ -1,7 +1,6 @@
 <?php
 interface IDb {
 	function connect($host, $user, $pass, $db, $port);
-	function init();
 	function escape_string($s, $strip_tags = true);
 	function query($query, $die_on_error = true);
 	function fetch_assoc($result);

+ 2 - 1
classes/logger/sql.php

@@ -6,6 +6,7 @@ class Logger_SQL {
 		if ($errno == E_NOTICE) return false;
 
 		if (Db::get()) {
+
 			$errno = Db::get()->escape_string($errno);
 			$errstr = Db::get()->escape_string($errstr);
 			$file = Db::get()->escape_string($file);
@@ -21,8 +22,8 @@ class Logger_SQL {
 				($errno, '$errstr', '$file', '$line', '$context', $owner_uid, NOW())");
 
 			return Db::get()->affected_rows($result) != 0;
-
 		}
+
 		return false;
 	}
 

+ 10 - 5
include/errorhandler.php

@@ -1,7 +1,7 @@
 <?php
 // TODO: make configurable
-//require_once "classes/logger.php";
-//require_once "classes/logger/sql.php";
+require_once "classes/logger.php";
+require_once "classes/logger/sql.php";
 
 function ttrss_error_handler($errno, $errstr, $file, $line, $context) {
 	global $logger;
@@ -37,11 +37,16 @@ function ttrss_fatal_handler() {
 		if (!$logger) $logger = new Logger_SQL();
 
 		if ($logger) {
-			$logger->log_error($errno, $errstr, $file, $line, $context);
+			if ($logger->log_error($errno, $errstr, $file, $line, $context)) {
+				return true;
+			}
 		}
+		return false;
 	}
+
+	return false;
 }
 
-//register_shutdown_function('ttrss_fatal_handler');
-//set_error_handler('ttrss_error_handler');
+register_shutdown_function('ttrss_fatal_handler');
+set_error_handler('ttrss_error_handler');
 ?>