reinstate error handlers; better DB error reporting on failed queries

This commit is contained in:
Andrew Dolgov 2013-04-17 16:05:52 +04:00
parent 404e2e3603
commit aca75cb5cb
6 changed files with 17 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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