#304924: extends drupal_error_handler to manage exceptions.

From: Damien Tournoud <damien@tournoud.net>

- Implement a _drupal_exception_handler, with a reimplementation of the "skip internal database functions" we had pre DB:TNG
 - Mark the handlers as private
 - Add supports for displaying functions and method names in the error message
 - General lifting of that key part of Drupal
---

 includes/common.inc            |  127 ++++++++++++++++++++++++++++++----------
 includes/database/database.inc |    7 --
 2 files changed, 96 insertions(+), 38 deletions(-)


diff --git includes/common.inc includes/common.inc
index 42cc5b7..ebe2601 100644
--- includes/common.inc
+++ includes/common.inc
@@ -583,50 +583,114 @@ function drupal_http_request($url, $headers = array(), $method = 'GET', $data =
  */
 
 /**
- * Log errors as defined by administrator.
+ * Custom PHP error handler: log errors as defined by administrator.
  *
- * Error levels:
- * - 0 = Log errors to database.
- * - 1 = Log errors to database and to screen.
+ * @param $error_level
+ *  The level of the error raised.
+ * @param $message
+ *  The error message.
+ * @param $filename
+ *  The filename that the error was raised in.
+ * @param $line
+ *  The line number the error was raised at.
+ * @param $context
+ *  An array that points to the active symbol table at the point the error occurred.
  */
-function drupal_error_handler($errno, $message, $filename, $line, $context) {
+function _drupal_error_handler($error_level, $message, $filename, $line, $context) {
   // If the @ error suppression operator was used, error_reporting will have
   // been temporarily set to 0.
   if (error_reporting() == 0) {
     return;
   }
+  if ($error_level & (E_ALL)) {
+    // All these constants are documented at http://php.net/manual/en/errorfunc.constants.php
+    $types = array(
+      E_ERROR => 'Error',
+      E_WARNING => 'Warning',
+      E_PARSE => 'Parse error',
+      E_NOTICE => 'Notice',
+      E_CORE_ERROR => 'Core error',
+      E_CORE_WARNING => 'Core warning',
+      E_COMPILE_ERROR => 'Compile error',
+      E_COMPILE_WARNING => 'Compile warning',
+      E_USER_ERROR => 'User error',
+      E_USER_WARNING => 'User warning',
+      E_USER_NOTICE => 'User notice',
+      E_STRICT => 'Strict warning',
+      E_RECOVERABLE_ERROR => 'Recoverable fatal error'
+    );
+    $backtrace = debug_backtrace();
+    _drupal_log_error(isset($types[$errno]) ? $types[$error_level] : 'Unknown error', $message, $backtrace);
+  }
+}
 
-  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', 4096 => 'recoverable fatal error');
-
-    // 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');
+/**
+ * Custom PHP exception handler: log uncaught exceptions and output a meaningful error message to the user.
+ *
+ * Uncaught exceptions are those not enclosed in a try/catch block. They are
+ * always fatal: the execution of the script will stop as soon as the exception
+ * handler exits.
+ *
+ * @param $exception
+ *   The exception object that was thrown.
+ */
+function _drupal_exception_handler($exception) {
+  $backtrace = $exception->getTrace();
+  // Add the line throwing the exception to the backtrace.
+  array_unshift($backtrace, array('line' => $exception->getLine(), 'file' => $exception->getFile()));
 
-      // 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;
-        }
-      }
+  // For PDOException errors, we try to return the initial caller,
+  // skipping internal functions of the database layer.
+  if ($exception instanceof PDOException) {
+    // The first element in the stack is the call, the second element gives us the caller.
+    // We skip calls that occured in one of the classes of the database layer
+    // or in one of its global functions.
+    $db_functions = array('db_query', 'pager_query', 'db_query_range', 'db_query_temporary', 'update_sql');
+    while (($caller = $backtrace[1]) &&
+         ((isset($caller['class']) && (strpos($caller['class'], 'Query') !== FALSE || strpos($caller['class'], 'Database') !== FALSE)) ||
+         in_array($caller['function'], $db_functions))) {
+      // We remove that call.
+      array_shift($backtrace);
     }
+  }
 
-    $entry = $types[$errno] . ': ' . $message . ' in ' . $filename . ' on line ' . $line . '.';
+  // Log the message to the watchdog and/or display it to the user.
+  _drupal_log_error('Uncaught exception', $exception->getMessage(), $backtrace);
 
-    // Force display of error messages in update.php.
-    if (variable_get('error_level', 1) == 1 || strstr($_SERVER['SCRIPT_NAME'], 'update.php')) {
-      drupal_set_message($entry, 'error');
-    }
+  // Uncaught exceptions are always fatal in PHP.
+  // We try to display an error message to the user.
+  if (!isset($GLOBALS['theme'])) {
+    drupal_maintenance_theme();
+    $type = 'maintenance_page';
+  }
+  else {
+    $type = 'page';
+  }
+  drupal_set_header('HTTP/1.1 503 Service unavailable');
+  drupal_set_title(t('Error'));
+  print theme($type, t('The website encountered an unexpected error. Please try again later.'));
+  exit;
+}
 
-    watchdog('php', '%message in %file on line %line.', array('%error' => $types[$errno], '%message' => $message, '%file' => $filename, '%line' => $line), WATCHDOG_ERROR);
+/**
+ * Helper function to log a PHP error or exception to the watchdog and to optionally display it to the user.
+ *
+ * @param $type
+ *   The type of the error (Error, Warning, ...).
+ * @param $message
+ *   The message associated to the error.
+ * @param $backtrace
+ *   The backtrace of function calls that led to this error.
+ */
+function _drupal_log_error($type, $message, $backtrace) {
+  $caller = _drupal_get_last_caller($backtrace);
+
+  // Force display of error messages in update.php.
+  if (variable_get('error_level', 1) == 1 || strstr($_SERVER['SCRIPT_NAME'], 'update.php')) {
+    drupal_set_message(t('@type: %message in %function (line %line of %file).', array('@type' => $type, '%message' => $message, '%function' => $caller['function'], '%line' => $caller['line'], '%file' => $caller['file'])), 'error');
   }
+
+  watchdog('php', '%error: %message in %function (line %line of %file).', array('%error' => $type, '%message' => $message, '%function' => $caller['function'], '%file' => $caller['file'], '%line' => $caller['line']), WATCHDOG_ERROR);  
 }
 
 /**
@@ -2514,7 +2578,8 @@ function _drupal_bootstrap_full() {
   require_once DRUPAL_ROOT . '/includes/mail.inc';
   require_once DRUPAL_ROOT . '/includes/actions.inc';
   // Set the Drupal custom error handler.
-  set_error_handler('drupal_error_handler');
+  set_error_handler('_drupal_error_handler');
+  set_exception_handler('_drupal_exception_handler');
   // Emit the correct charset HTTP header.
   drupal_set_header('Content-Type: text/html; charset=utf-8');
   // Detect string handling method
diff --git includes/database/database.inc includes/database/database.inc
index afa772d..cd16695 100644
--- includes/database/database.inc
+++ includes/database/database.inc
@@ -7,13 +7,6 @@
  */
 
 /**
- * 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.
