Merge pull request #5487 from eileenmcnaughton/CRM-16165
[civicrm-core.git] / CRM / Core / Error.php
index ae0bab6e0de08849c2e03af470dda277e46d5ad7..cb4c217a3a1cbd3de5016f7e2ca5ee7456620d8d 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * Start of the Error framework. We should check out and inherit from
@@ -64,6 +64,7 @@ class CRM_Exception extends PEAR_Exception {
   public function __construct($message = NULL, $code = 0, Exception $previous = NULL) {
     parent::__construct($message, $code, $previous);
   }
+
 }
 
 /**
@@ -72,7 +73,7 @@ class CRM_Exception extends PEAR_Exception {
 class CRM_Core_Error extends PEAR_ErrorStack {
 
   /**
-   * Status code of various types of errors
+   * Status code of various types of errors.
    */
   const FATAL_ERROR = 2;
   const DUPLICATE_CONTACT = 8001;
@@ -87,7 +88,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   private static $_singleton = NULL;
 
   /**
-   * The logger object for this application
+   * The logger object for this application.
    * @var object
    */
   private static $_log = NULL;
@@ -116,7 +117,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Constructor
+   * Constructor.
    */
   public function __construct() {
     parent::__construct('CiviCRM');
@@ -148,7 +149,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Status display function specific to payment processor errors
+   * Status display function specific to payment processor errors.
    * @param $error
    * @param string $separator
    */
@@ -191,7 +192,16 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     $error['type'] = $pearError->getType();
     $error['user_info'] = $pearError->getUserInfo();
     $error['to_string'] = $pearError->toString();
-    if (function_exists('mysql_error') &&
+
+    // We access connection info via _DB_DATAOBJECT instead
+    // of, e.g., calling getDatabaseConnection(), so that we
+    // can avoid infinite loops.
+    global $_DB_DATAOBJECT;
+
+    if (!isset($_DB_DATAOBJECT['CONFIG']['database'])) {
+      // we haven't setup sql, so it's not our sql error...
+    }
+    elseif (preg_match('/^mysql:/', $_DB_DATAOBJECT['CONFIG']['database']) &&
       mysql_error()
     ) {
       $mysql_error = mysql_error() . ', ' . mysql_errno();
@@ -200,13 +210,9 @@ class CRM_Core_Error extends PEAR_ErrorStack {
       // execute a dummy query to clear error stack
       mysql_query('select 1');
     }
-    elseif (function_exists('mysqli_error')) {
+    elseif (preg_match('/^mysqli:/', $_DB_DATAOBJECT['CONFIG']['database'])) {
       $dao = new CRM_Core_DAO();
 
-      // we do it this way, since calling the function
-      // getDatabaseConnection could potentially result
-      // in an infinite loop
-      global $_DB_DATAOBJECT;
       if (isset($_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5])) {
         $conn = $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5];
         $link = $conn->connection;
@@ -291,7 +297,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Display an error page with an error message describing what happened
+   * Display an error page with an error message describing what happened.
    *
    * @param string $message
    *   The error message.
@@ -380,7 +386,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Display an error page with an error message describing what happened
+   * Display an error page with an error message describing what happened.
    *
    * This function is evil -- it largely replicates fatal(). Hopefully the
    * entire CRM_Core_Error system can be hollowed out and replaced with
@@ -559,7 +565,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Display the error message on terminal
+   * Display the error message on terminal.
    *
    * @param $message
    * @param bool $out
@@ -586,7 +592,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     if ($config->userFrameworkLogging) {
       // should call $config->userSystem->logger($message) here - but I got a situation where userSystem was not an object - not sure why
       if ($config->userSystem->is_drupal and function_exists('watchdog')) {
-        watchdog('civicrm', $message, NULL, WATCHDOG_DEBUG);
+        watchdog('civicrm', '%message', array('%message' => $message), WATCHDOG_DEBUG);
       }
     }
 
@@ -622,7 +628,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Obtain a reference to the error log
+   * Obtain a reference to the error log.
    *
    * @param string $comp
    *
@@ -672,7 +678,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Render a backtrace array as a string
+   * Render a backtrace array as a string.
    *
    * @param array $backTrace
    *   Array of stack frames.
@@ -693,7 +699,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Render a backtrace array as an array
+   * Render a backtrace array as an array.
    *
    * @param array $backTrace
    *   Array of stack frames.
@@ -771,7 +777,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Render an exception as HTML string
+   * Render an exception as HTML string.
    *
    * @param Exception $e
    * @return string
@@ -807,7 +813,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Write details of an exception to the log
+   * Write details of an exception to the log.
    *
    * @param Exception $e
    * @return string
@@ -869,7 +875,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Reset the error stack
+   * Reset the error stack.
    *
    */
   public static function reset() {
@@ -943,7 +949,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Terminate execution abnormally
+   * Terminate execution abnormally.
    */
   protected static function abend($code) {
     // do a hard rollback of any pending transactions
@@ -967,6 +973,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     }
     return FALSE;
   }
+
 }
 
 $e = new PEAR_ErrorStack('CRM');