From 1b3d7ff9bda710f9c840ba38b8f65d4aec0bae50 Mon Sep 17 00:00:00 2001
From: diosmosis <benaka@piwik.pro>
Date: Fri, 4 Sep 2015 19:39:53 -0700
Subject: [PATCH] Catch exceptions when getting individual reports during
 scheduled reports generation so reports will still be sent even if individual
 reports fail in some way.

---
 plugins/ScheduledReports/API.php | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/plugins/ScheduledReports/API.php b/plugins/ScheduledReports/API.php
index f77fa4b223..d5a985e65b 100644
--- a/plugins/ScheduledReports/API.php
+++ b/plugins/ScheduledReports/API.php
@@ -26,6 +26,9 @@ use Piwik\Site;
 use Piwik\Tracker;
 use Piwik\Translate;
 use Piwik\Translation\Translator;
+use Piwik\Url;
+use Piwik\UrlHelper;
+use Psr\Log\LoggerInterface;
 
 /**
  * The ScheduledReports API lets you manage Scheduled Email reports, as well as generate, download or email any existing report.
@@ -60,6 +63,16 @@ class API extends \Piwik\Plugin\API
     // static cache storing reports
     public static $cache = array();
 
+    /**
+     * @var LoggerInterface
+     */
+    private $logger;
+
+    public function __construct(LoggerInterface $logger)
+    {
+        $this->logger = $logger;
+    }
+
     /**
      * Creates a new report and schedules it.
      *
@@ -377,7 +390,20 @@ class API extends \Piwik\Plugin\API
                 $params['segment'] = false;
             }
 
-            $processedReport = Request::processRequest('API.getProcessedReport', $params);
+            try {
+                $processedReport = Request::processRequest('API.getProcessedReport', $params);
+            } catch (\Exception $ex) {
+                // NOTE: can't use warning or error because the log message will appear in the UI as a notification
+                $this->logger->info("Error getting '?{report}' when generating scheduled report: {exception}", array(
+                    'report' => http_build_query($params),
+                    'exception' => $ex->getMessage(),
+                ));
+
+                $this->logger->debug($ex);
+
+                continue;
+            }
+
             $processedReport['segment'] = $segment;
 
             // TODO add static method getPrettyDate($period, $date) in Period
-- 
GitLab