From 0a7b323e816c03995d9d8bc3e7d277005a315a77 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Fri, 4 Mar 2011 16:49:17 -0500 Subject: [PATCH] you can now specify your own error handling function by changing DB::$error_handler --- db.class.php | 29 ++++++++++++++++++++++++----- simpletest/BasicTest.php | 16 ++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/db.class.php b/db.class.php index d9531df..4eaf1a6 100644 --- a/db.class.php +++ b/db.class.php @@ -17,6 +17,7 @@ class DB public static $host = 'localhost'; public static $encoding = 'latin1'; public static $queryMode = 'queryAllRows'; //buffered, unbuffered, queryAllRows + public static $error_handler = 'meekrodb_error_handler'; public static function get($dbName = '') { static $mysql = null; @@ -290,14 +291,16 @@ class DB $result = $db->query($sql, $is_buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); if (DB::$debug) $runtime = microtime(true) - $starttime; - $sqlShow = "$sql (" . ($is_buffered ? 'MYSQLI_STORE_RESULT' : 'MYSQLI_USE_RESULT') . ")"; if (!$sql || $error = DB::checkError()) { - echo "ATTEMPTED QUERY: $sqlShow
\n"; - echo "ERROR: $error
\n"; - debug_print_backtrace(); - die; + if (function_exists(DB::$error_handler)) { + call_user_func(DB::$error_handler, array( + 'query' => $sql, + 'error' => $error + )); + } } else if (DB::$debug) { $runtime = sprintf('%f', $runtime * 1000); + $sqlShow = "$sql (" . ($is_buffered ? 'MYSQLI_STORE_RESULT' : 'MYSQLI_USE_RESULT') . ")"; echo "QUERY: $sqlShow [$runtime ms]
\n"; } @@ -482,4 +485,20 @@ class DBTransaction { } +function meekrodb_error_handler($params) { + $out[] = "QUERY: " . $params['query']; + $out[] = "ERROR: " . $params['error']; + $out[] = ""; + + if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { + echo implode("\n", $out); + } else { + echo implode("
\n", $out); + } + + debug_print_backtrace(); + + die; +} + ?> diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index c7c4d05..559aae9 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -1,4 +1,11 @@ assert(count($results) === 2); } + + function test_5_error_handler() { + global $error_callback_worked; + + DB::$error_handler = 'new_error_callback'; + DB::query("SELET * FROM accounts"); + + $this->assert($error_callback_worked === 1); + } }