From aca75cb5cb323535099c7aef46a78ea3cec082f2 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 17 Apr 2013 16:05:52 +0400 Subject: [PATCH] reinstate error handlers; better DB error reporting on failed queries --- classes/db.php | 7 +------ classes/db/mysql.php | 6 ++---- classes/db/pgsql.php | 5 ++--- classes/idb.php | 1 - classes/logger/sql.php | 3 ++- include/errorhandler.php | 15 ++++++++++----- 6 files changed, 17 insertions(+), 20 deletions(-) diff --git a/classes/db.php b/classes/db.php index 558d3e6b7..c3b627096 100644 --- a/classes/db.php +++ b/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; diff --git a/classes/db/mysql.php b/classes/db/mysql.php index 64c35ebdc..241d2a063 100644 --- a/classes/db/mysql.php +++ b/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 $query 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; } diff --git a/classes/db/pgsql.php b/classes/db/pgsql.php index 0f38fb8cb..bafd54ab2 100644 --- a/classes/db/pgsql.php +++ b/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 $query 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; } diff --git a/classes/idb.php b/classes/idb.php index 1ca6925b4..16f760bf6 100644 --- a/classes/idb.php +++ b/classes/idb.php @@ -1,7 +1,6 @@ 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; } diff --git a/include/errorhandler.php b/include/errorhandler.php index 45496b18b..b1a0d3d0c 100644 --- a/include/errorhandler.php +++ b/include/errorhandler.php @@ -1,7 +1,7 @@ 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'); ?>