Fix Error handling following DB Package upgrade
authorSeamus Lee <seamuslee001@gmail.com>
Sun, 5 Jan 2020 20:44:35 +0000 (07:44 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Sun, 5 Jan 2020 20:44:35 +0000 (07:44 +1100)
CRM/Core/Error.php

index d99104c2027343099d01356a1b1771977170032f..1f0b4d40dbf2e9a81b90e1e90922469605989ca5 100644 (file)
@@ -803,16 +803,19 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     if ($e instanceof PEAR_Exception) {
       $ei = $e;
       while (is_callable([$ei, 'getCause'])) {
-        if ($ei->getCause() instanceof PEAR_Error) {
-          $msg .= '<table class="crm-db-error">';
-          $msg .= sprintf('<thead><tr><th>%s</th><th>%s</th></tr></thead>', ts('Error Field'), ts('Error Value'));
-          $msg .= '<tbody>';
-          foreach (['Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo'] as $f) {
-            $msg .= sprintf('<tr><td>%s</td><td>%s</td></tr>', $f, call_user_func([$ei->getCause(), "get$f"]));
+        // DB_ERROR doesn't have a getCause but does have a __call function which tricks is_callable.
+        if (!$ei instanceof DB_Error) {
+          if ($ei->getCause() instanceof PEAR_Error) {
+            $msg .= '<table class="crm-db-error">';
+            $msg .= sprintf('<thead><tr><th>%s</th><th>%s</th></tr></thead>', ts('Error Field'), ts('Error Value'));
+            $msg .= '<tbody>';
+            foreach (['Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo'] as $f) {
+              $msg .= sprintf('<tr><td>%s</td><td>%s</td></tr>', $f, call_user_func([$ei->getCause(), "get$f"]));
+            }
+            $msg .= '</tbody></table>';
           }
-          $msg .= '</tbody></table>';
+          $ei = $ei->getCause();
         }
-        $ei = $ei->getCause();
       }
       $msg .= $e->toHtml();
     }
@@ -835,12 +838,19 @@ class CRM_Core_Error extends PEAR_ErrorStack {
 
     $ei = $e;
     while (is_callable([$ei, 'getCause'])) {
-      if ($ei->getCause() instanceof PEAR_Error) {
-        foreach (['Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo'] as $f) {
-          $msg .= sprintf(" * ERROR %s: %s\n", strtoupper($f), call_user_func([$ei->getCause(), "get$f"]));
+      // DB_ERROR doesn't have a getCause but does have a __call function which tricks is_callable.
+      if (!$ei instanceof DB_Error) {
+        if ($ei->getCause() instanceof PEAR_Error) {
+          foreach (['Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo'] as $f) {
+            $msg .= sprintf(" * ERROR %s: %s\n", strtoupper($f), call_user_func([$ei->getCause(), "get$f"]));
+          }
         }
+        $ei = $ei->getCause();
+      }
+      // if we have reached a DB_Error assume that is the end of the road.
+      else {
+        $ei = NULL;
       }
-      $ei = $ei->getCause();
     }
     $msg .= self::formatBacktrace($e->getTrace());
     return $msg;