From 059f23755b2f575dce1a1b3ddeb291faf37b6188 Mon, 28 Feb 2011 03:01:38 +0100
From: Bernd Oliver Suenderhauf <pancho@suenderhauf.net>
Date: Mon, 28 Feb 2011 02:59:34 +0100
Subject: [PATCH] Issue #1075386 by Pancho: Fix and refactor amazon_store_report_error()

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 362322d..3444e87 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,10 +1,13 @@
 CHANGELOG for Amazon Store
 
+amazon_store-7.x-1.x-beta4
+--------------------------
+Issue [#1075386] by Pancho: Fix and refactor amazon_store_report_error()
 Issue [#1068600] (followup) by Pancho: Streamline 'CartModify' parameters
 Issue [#1074294] by Pancho: Fix 'CartCreate' parameters
 
 amazon_store-7.x-1.x-beta3
-------------------------
+--------------------------
 [#989680] by rfay: Avoid warnings after changing locale
 [#1068600] by Pancho: Remove MergeCart parameter and rewrite handling of cart credentials
 [#1065370] by Pancho: Split off admin page from main module
diff --git a/amazon_store.module b/amazon_store.module
index 013012b..80bd8de 100644
--- a/amazon_store.module
+++ b/amazon_store.module
@@ -628,7 +628,7 @@
     }
     else {
       drupal_set_message(t("Failed to update cart quantity"));
-      watchdog('amazon', "Failed to update cart quantity. Errors=%errors", array('%errors' => print_r($results->Cart->Request->Errors, TRUE)));
+      amazon_store_report_error('Failed to update cart quantity.', $results->Cart->Request->Errors);
       return FALSE;
     }
   }
@@ -657,7 +657,7 @@
   if ($cart_creds = amazon_store_get_cart_creds()) {
     $results = amazon_store_http_request('CartClear', $cart_creds);
     if (!empty($results->error)) {
-      watchdog('amazon', "Failed to clear shopping cart");
+      amazon_store_report_error('Failed to clear cart.', $results->Cart->Request->Errors);
       drupal_set_message(t("failed to clear shopping cart"));
       return;
     }
@@ -678,7 +678,7 @@
     $results = amazon_store_http_request('CartGet', $cart_creds);
     // Check results
     if (!empty($results->error) || $results->Cart->Request->IsValid != 'True') {
-      amazon_store_report_error($results->Cart->Request->Errors);
+      amazon_store_report_error('Failed to get cart', $results->Cart->Request->Errors);
       drupal_set_message(t("Failed to get cart"));
     }
   }
@@ -762,14 +762,21 @@
  * Utility function for reporting errors as reported in the
  * errors portion of SimpleXML Amazon item object.
  *
- * @param $errors
+ * @param $message Custom error message
+ * @param $errors SimpleXML object containing errors
  */
-function amazon_store_report_error($errors) {
-  foreach ($errors->Error as $error) {
-    $i++;
-    $msg .= "($i): {$error->Code}: {$error->Message} <br/>";
-  }
-  watchdog('amazon', 'Error processing SimpleXML: @msg', array('@msg' => $msg));
+function amazon_store_report_error($message = 'Amazon request failed.', $errors = NULL) {
+	if ($errors) {
+		$message .= " Amazon's response was: <br /><ul>";
+		$msglines = array();
+	  foreach ($errors->Error as $error) {
+	  	$i++;
+	    $message .= "<li>%msg$i</li>";
+	  	$msglines["%msg$i"] = "[{$error->Code}]: {$error->Message}";
+	  }
+	  $message .= '</ul>';
+	}
+	watchdog('amazon_store', $message, $msglines);
 }
 
 /**
