jenkins-bot has submitted this change and it was merged.

Change subject: Alter WMFException to permit us to present a simpler error for 
users.
......................................................................


Alter WMFException to permit us to present a simpler error for users.

Currently the debug is being passed in, which is fine except when we want to 
present the message to users.

Bug: T88460
Change-Id: Icd69c23dd4387aff720cdc9d6b0e5950406e4067
---
M sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
M sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
M sites/all/modules/thank_you/thank_you.module
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
M sites/all/modules/wmf_common/WmfException.php
5 files changed, 43 insertions(+), 22 deletions(-)

Approvals:
  Ejegg: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module 
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
index ab4cf64..710e64f 100644
--- a/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
+++ b/sites/all/modules/recurring_globalcollect/recurring_globalcollect.module
@@ -274,7 +274,7 @@
       }
       catch (Exception $e) {
           $message = 'Batch processing aborted: ' . $e->getMessage();
-          $e = new WmfException( 'UNKNOWN', $message, $contribution_recur);
+          $e = new WmfException( 'UNKNOWN', $message, 
array('contribution_recur' => $contribution_recur));
           $failed[] = $contribution_recur;
           break;
       }
@@ -337,7 +337,7 @@
 
   if ( $result->isFailed() || $result->getErrors() ) {
     _recurring_globalcollect_update_record_failure($contribution_recur_id);
-    throw new WmfException( 'PAYMENT_FAILED', 'recurring charge failed', 
$result);
+    throw new WmfException( 'PAYMENT_FAILED', 'recurring charge failed', 
array('payment_result' => $result));
   }
 
   // If success, add a record to the contribution table and send a thank you 
email.
@@ -370,7 +370,7 @@
     try {
         $transaction = WmfTransaction::from_unique_id( 
$contribution_recur['trxn_id'] );
     } catch ( Exception $ex ) {
-        throw new WmfException( 'INVALID_RECURRING', $ex->getMessage(), 
$contribution_recur );
+        throw new WmfException( 'INVALID_RECURRING', $ex->getMessage(), 
array('contribution_recur' => $contribution_recur ));
     }
 
     $msg = array(
diff --git 
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc 
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
index eeaa298..99dae1a 100644
--- 
a/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
+++ 
b/sites/all/modules/recurring_globalcollect/recurring_globalcollect_common.inc
@@ -240,7 +240,7 @@
       civicrm_api_contribution_status( 'Failed' ),
   );
   if ( !in_array( $record['contribution_status_id'], $working_statuses ) ) {
-    throw new WmfException( 'INVALID_RECURRING', t( 'The subscription is 
supposed to be in a completed or failed state before it can be processed.' ), 
$record );
+    throw new WmfException( 'INVALID_RECURRING', t( 'The subscription is 
supposed to be in a completed or failed state before it can be processed.' ), 
array('contribution_recur' => $record) );
   }
 
   $dbs = wmf_civicrm_get_dbs();
@@ -257,7 +257,7 @@
   $dbs->pop();
 
   if ( !$affected_rows ) {
-    throw new WmfException( 'INVALID_RECURRING', t( 'The subscription was not 
marked as in progress.' ), $record );
+    throw new WmfException( 'INVALID_RECURRING', t( 'The subscription was not 
marked as in progress.' ), array('contribution_recur' => $record) );
   }
 
   return $affected_rows;
@@ -301,9 +301,9 @@
 
 /**
  * _recurring_globalcollect_validate_record_for_update
- * 
+ *
  * @param array $record
- * @throws Exception 
+ * @throws Exception
  * @return boolean
  */
 function _recurring_globalcollect_validate_record_for_update($record) {
@@ -324,25 +324,25 @@
   // Make sure $cycle_day is not empty
   if (empty($cycle_day)) {
     $message = 'cycle_day cannot be empty';
-    throw new WmfException( 'INVALID_RECURRING', $message, $record );
+    throw new WmfException( 'INVALID_RECURRING', $message, 
array('contribution_recur' => $record) );
   }
 
   // Make sure $frequency_interval is not empty
   if (empty($frequency_interval)) {
     $message = 'frequency_interval cannot be empty';
-    throw new WmfException( 'INVALID_RECURRING', $message, $record );
+    throw new WmfException( 'INVALID_RECURRING', $message, 
array('contribution_recur' => $record) );
   }
 
   // Make sure a valid interval is assigned
   if (!in_array($frequency_unit, $allowed_intervals)) {
     $message = 'Invalid frequency_unit [' . $frequency_unit . '] for 
recurring_globalcollect. Allowed intervals: [ ' . implode(', ', 
$allowed_intervals) . ' ]';
-    throw new WmfException( 'INVALID_RECURRING', $message, $record );
+    throw new WmfException( 'INVALID_RECURRING', $message, 
array('contribution_recur' => $record) );
   }
 
   // Make sure $next_sched_contribution is assigned
   if (empty($next_sched_contribution)) {
     $message = 'next_sched_contribution_date cannot be empty';
-    throw new WmfException( 'INVALID_RECURRING', $message, $record );
+    throw new WmfException( 'INVALID_RECURRING', $message, 
array('contribution_recur' => $record));
   }
 }
 
diff --git a/sites/all/modules/thank_you/thank_you.module 
b/sites/all/modules/thank_you/thank_you.module
index 714ffc5..12f7404 100644
--- a/sites/all/modules/thank_you/thank_you.module
+++ b/sites/all/modules/thank_you/thank_you.module
@@ -451,7 +451,7 @@
 
        if( $email['html'] == false ){
                $msg = "HTML rendering of template failed in 
{$params['locale']}.";
-               throw new WmfException( 'UNKNOWN', $msg, $params );
+               throw new WmfException( 'UNKNOWN', $msg, 
array('thank_you_params' => $params) );
        }
 
        $email['subject'] = thank_you_get_subject( $params['locale'] );
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 991d2dd..269cbb1 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -450,7 +450,7 @@
         throw new WmfException(
             'INVALID_MESSAGE',
             'Cannot create contribution, civi error!',
-            $e->getMessage() . print_r($e->getExtraParams(), TRUE)
+            $e->getExtraParams()
         );
     }
 
@@ -1396,7 +1396,7 @@
 
     $result = civicrm_api( "Email", "Create", $email_params );
     if ( array_key_exists( 'is_error', $result ) && $result['is_error'] != 0 ) 
{
-        throw new WmfException( 'IMPORT_CONTACT', "Couldn't store email for 
the contact.", $result );
+        throw new WmfException( 'IMPORT_CONTACT', "Couldn't store email for 
the contact.", array('result' => $result) );
     }
 }
 
@@ -1431,7 +1431,7 @@
 
     $result = CRM_Core_BAO_Address::add( $address_params, false );
     if ( !$result ) {
-        throw new WmfException( 'IMPORT_CONTACT', "Couldn't store address for 
the contact.", $result );
+        throw new WmfException( 'IMPORT_CONTACT', "Couldn't store address for 
the contact.");
     }
 }
 
diff --git a/sites/all/modules/wmf_common/WmfException.php 
b/sites/all/modules/wmf_common/WmfException.php
index cd5d3b8..6e00b68 100644
--- a/sites/all/modules/wmf_common/WmfException.php
+++ b/sites/all/modules/wmf_common/WmfException.php
@@ -107,8 +107,22 @@
 
     var $extra;
     var $type;
+    var $userMessage;
 
-    function __construct( $type, $message, $extra = null ) {
+  /**
+   * WmfException constructor.
+   *
+   * @param string $type Error type
+   * @param int $message A WMF constructed message.
+   * @param array $extra Extra parameters.
+   *   If error_message is included then it will be included in the User Error 
message.
+   *   If you are working with a CiviCRM Exception ($e) then you can pass in 
$e->getExtraParams()
+   *   which will include the api error message and message and potentially 
backtrace & sql
+   *   details (if you passed in 'debug' => 1).
+   *   Any data in the $extra array will be rendered in fail mails - but only 
'error_message'
+   *   is used for user messages (provided the getUserMessage function is 
used).
+   */
+    function __construct( $type, $message, $extra = array()) {
         if ( !array_key_exists( $type, self::$error_types ) ) {
             $message .= ' -- ' . t( 'Warning, throwing a misspelled exception: 
"%type"', array( '%type' => $type ) );
             $type = 'UNKNOWN';
@@ -121,22 +135,29 @@
             $message = implode( "\n", $message );
         }
         $this->message = "{$this->type} {$message}";
-
-        if ( $extra ) {
-            $this->message .= "\nSource: " . var_export( $extra, true );
-        }
+        $this->userMessage = $this->message;
+        $this->message = $this->message . "\nSource: " . var_export( 
$this->extra, true );
 
         if ( function_exists( 'watchdog' ) ) {
             // It seems that dblog_watchdog will pass through XSS, so
             // rely on our own escaping above, rather than pass $vars.
-            $escaped = htmlspecialchars( $this->message, ENT_COMPAT, 'UTF-8', 
false );
+            $escaped = htmlspecialchars( $this->getMessage(), ENT_COMPAT, 
'UTF-8', false );
             watchdog( 'wmf_common', $escaped, NULL, WATCHDOG_ERROR );
         }
         if ( function_exists('drush_set_error') && $this->isFatal() ) {
-            drush_set_error( $this->type, $this->message );
+            drush_set_error( $this->type, $this->getMessage() );
         }
     }
 
+  /**
+   * Get error message intended for end users.
+   *
+   * @return string
+   */
+    function getUserErrorMessage() {
+        return !empty($this->extra['error_message']) ? 
$this->extra['error_message'] : $this->userMessage;
+    }
+
     function getErrorName()
     {
         return $this->type;

-- 
To view, visit https://gerrit.wikimedia.org/r/319757
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icd69c23dd4387aff720cdc9d6b0e5950406e4067
Gerrit-PatchSet: 2
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Eileen <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Cdentinger <[email protected]>
Gerrit-Reviewer: Eileen <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to