summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGábor Hojtsy <gabor@hojtsy.hu>2007-08-29 18:38:55 +0000
committerGábor Hojtsy <gabor@hojtsy.hu>2007-08-29 18:38:55 +0000
commit005a583f326fd48f501ec6569e00ebcdfb66b7ac (patch)
treed60cd0160daace079b6c612380a88b5abcd98c7c
parent94c1729eacb262598aa75fda35049b344051012b (diff)
downloadbrdo-005a583f326fd48f501ec6569e00ebcdfb66b7ac.tar.gz
brdo-005a583f326fd48f501ec6569e00ebcdfb66b7ac.tar.bz2
#168812 by webchick and pwolanin: in case we have a database error, trace it back to the original database function call, so we can provide a more accurate error message for DB errors
-rw-r--r--includes/common.inc22
-rw-r--r--includes/database.inc7
-rw-r--r--includes/database.mysql.inc2
-rw-r--r--includes/database.mysqli.inc2
-rw-r--r--includes/database.pgsql.inc2
5 files changed, 34 insertions, 1 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 3619c325b..9cbf27e45 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -531,7 +531,7 @@ function drupal_http_request($url, $headers = array(), $method = 'GET', $data =
* 0 = Log errors to database.
* 1 = Log errors to database and to screen.
*/
-function drupal_error_handler($errno, $message, $filename, $line) {
+function drupal_error_handler($errno, $message, $filename, $line, $context) {
// If the @ error suppression operator was used, error_reporting is temporarily set to 0
if (error_reporting() == 0) {
return;
@@ -539,6 +539,26 @@ function drupal_error_handler($errno, $message, $filename, $line) {
if ($errno & (E_ALL)) {
$types = array(1 => 'error', 2 => 'warning', 4 => 'parse error', 8 => 'notice', 16 => 'core error', 32 => 'core warning', 64 => 'compile error', 128 => 'compile warning', 256 => 'user error', 512 => 'user warning', 1024 => 'user notice', 2048 => 'strict warning');
+
+ // For database errors, we want the line number/file name of the place that
+ // the query was originally called, not _db_query().
+ if (isset($context[DB_ERROR])) {
+ $backtrace = array_reverse(debug_backtrace());
+
+ // List of functions where SQL queries can originate.
+ $query_functions = array('db_query', 'pager_query', 'db_query_range', 'db_query_temporary', 'update_sql');
+
+ // Determine where query function was called, and adjust line/file
+ // accordingly.
+ foreach ($backtrace as $index => $function) {
+ if (in_array($function['function'], $query_functions)) {
+ $line = $backtrace[$index]['line'];
+ $filename = $backtrace[$index]['file'];
+ break;
+ }
+ }
+ }
+
$entry = $types[$errno] .': '. $message .' in '. $filename .' on line '. $line .'.';
// Force display of error messages in update.php
diff --git a/includes/database.inc b/includes/database.inc
index ee60b80d9..142466536 100644
--- a/includes/database.inc
+++ b/includes/database.inc
@@ -7,6 +7,13 @@
*/
/**
+ * A hash value to check when outputting database errors, md5('DB_ERROR').
+ *
+ * @see drupal_error_handler
+ */
+define('DB_ERROR', 'a515ac9c2796ca0e23adbe92c68fc9fc');
+
+/**
* @defgroup database Database abstraction layer
* @{
* Allow the use of different database servers using the same code base.
diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc
index 20cb9ae80..a5bee5a87 100644
--- a/includes/database.mysql.inc
+++ b/includes/database.mysql.inc
@@ -154,6 +154,8 @@ function _db_query($query, $debug = 0) {
return $result;
}
else {
+ // Indicate to drupal_error_handler that this is a database error.
+ ${DB_ERROR} = TRUE;
trigger_error(check_plain(mysql_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
return FALSE;
}
diff --git a/includes/database.mysqli.inc b/includes/database.mysqli.inc
index b51502a75..7ca01d15e 100644
--- a/includes/database.mysqli.inc
+++ b/includes/database.mysqli.inc
@@ -151,6 +151,8 @@ function _db_query($query, $debug = 0) {
return $result;
}
else {
+ // Indicate to drupal_error_handler that this is a database error.
+ ${DB_ERROR} = TRUE;
trigger_error(check_plain(mysqli_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
return FALSE;
}
diff --git a/includes/database.pgsql.inc b/includes/database.pgsql.inc
index 23fd2b8d6..36ca5689a 100644
--- a/includes/database.pgsql.inc
+++ b/includes/database.pgsql.inc
@@ -171,6 +171,8 @@ function _db_query($query, $debug = 0) {
return $last_result;
}
else {
+ // Indicate to drupal_error_handler that this is a database error.
+ ${DB_ERROR} = TRUE;
trigger_error(check_plain(pg_last_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
return FALSE;
}