reinstate error handlers; better DB error reporting on failed queries

master
Andrew Dolgov 12 years ago
parent 404e2e3603
commit aca75cb5cb

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

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

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

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

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

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

Loading…
Cancel
Save