Commit 005a583f authored by Gábor Hojtsy's avatar Gábor Hojtsy
Browse files

#168812 by webchick and pwolanin: in case we have a database error, trace it...

#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
parent 94c1729e
...@@ -531,7 +531,7 @@ function drupal_http_request($url, $headers = array(), $method = 'GET', $data = ...@@ -531,7 +531,7 @@ function drupal_http_request($url, $headers = array(), $method = 'GET', $data =
* 0 = Log errors to database. * 0 = Log errors to database.
* 1 = Log errors to database and to screen. * 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 the @ error suppression operator was used, error_reporting is temporarily set to 0
if (error_reporting() == 0) { if (error_reporting() == 0) {
return; return;
...@@ -539,6 +539,26 @@ function drupal_error_handler($errno, $message, $filename, $line) { ...@@ -539,6 +539,26 @@ function drupal_error_handler($errno, $message, $filename, $line) {
if ($errno & (E_ALL)) { 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'); $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 .'.'; $entry = $types[$errno] .': '. $message .' in '. $filename .' on line '. $line .'.';
// Force display of error messages in update.php // Force display of error messages in update.php
......
...@@ -6,6 +6,13 @@ ...@@ -6,6 +6,13 @@
* Wrapper for database interface code. * Wrapper for database interface code.
*/ */
/**
* 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 * @defgroup database Database abstraction layer
* @{ * @{
......
...@@ -154,6 +154,8 @@ function _db_query($query, $debug = 0) { ...@@ -154,6 +154,8 @@ function _db_query($query, $debug = 0) {
return $result; return $result;
} }
else { 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); trigger_error(check_plain(mysql_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
return FALSE; return FALSE;
} }
......
...@@ -151,6 +151,8 @@ function _db_query($query, $debug = 0) { ...@@ -151,6 +151,8 @@ function _db_query($query, $debug = 0) {
return $result; return $result;
} }
else { 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); trigger_error(check_plain(mysqli_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
return FALSE; return FALSE;
} }
......
...@@ -171,6 +171,8 @@ function _db_query($query, $debug = 0) { ...@@ -171,6 +171,8 @@ function _db_query($query, $debug = 0) {
return $last_result; return $last_result;
} }
else { 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); trigger_error(check_plain(pg_last_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
return FALSE; return FALSE;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment