[icinga-checkins] icinga.org: icingaweb2/master: Dashboard: Show error message when can not write to file

git at icinga.org git at icinga.org
Wed Nov 19 12:15:44 CET 2014


Module: icingaweb2
Branch: master
Commit: a40f357f3cf5abf5671f66c5d509d2db3e9736db
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=a40f357f3cf5abf5671f66c5d509d2db3e9736db

Author: Marius Hein <marius.hein at netways.de>
Date:   Wed Nov 19 11:47:31 2014 +0100

Dashboard: Show error message when can not write to file

refs #4537

---

 application/controllers/DashboardController.php    |   40 +++++++++++++++++---
 application/views/scripts/dashboard/error.phtml    |   13 +++++++
 .../views/scripts/dashboard/new-component.phtml    |    1 -
 library/Icinga/File/Ini/IniWriter.php              |   10 +++++
 library/Icinga/Web/Widget/Dashboard.php            |   17 +++++++--
 5 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php
index 105b407..94a6b38 100644
--- a/application/controllers/DashboardController.php
+++ b/application/controllers/DashboardController.php
@@ -44,7 +44,8 @@ class DashboardController extends ActionController
             $params['url'] = rawurldecode($this->_request->getParam('url'));
             $form->populate($params);
         }
-        $form->setOnSuccess(function (Form $form) use ($dashboard) {
+        $action = $this;
+        $form->setOnSuccess(function (Form $form) use ($dashboard, $action) {
             try {
                 $pane = $dashboard->getPane($form->getValue('pane'));
             } catch (ProgrammingError $e) {
@@ -55,7 +56,14 @@ class DashboardController extends ActionController
             $component = new Dashboard\Component($form->getValue('component'), $form->getValue('url'), $pane);
             $component->setUserWidget();
             $pane->addComponent($component);
-            $dashboard->write();
+            try {
+                $dashboard->write();
+            } catch (\Zend_Config_Exception $e) {
+                $action->view->error = $e;
+                $action->view->config = $dashboard->createWriter();
+                $action->render('error');
+                return false;
+            }
             Notification::success(t('Component created'));
             return true;
         });
@@ -83,7 +91,8 @@ class DashboardController extends ActionController
                 400
             );
         }
-        $form->setOnSuccess(function (Form $form) use ($dashboard) {
+        $action = $this;
+        $form->setOnSuccess(function (Form $form) use ($dashboard, $action) {
             try {
                 $pane = $dashboard->getPane($form->getValue('pane'));
             } catch (ProgrammingError $e) {
@@ -108,7 +117,14 @@ class DashboardController extends ActionController
                 $oldPane = $dashboard->getPane($form->getValue('org_pane'));
                 $oldPane->removeComponent($component->getTitle());
             }
-            $dashboard->write();
+            try {
+                $dashboard->write();
+            } catch (\Zend_Config_Exception $e) {
+                $action->view->error = $e;
+                $action->view->config = $dashboard->createWriter();
+                $action->render('error');
+                return false;
+            }
             Notification::success(t('Component updated'));
             return true;
         });
@@ -140,13 +156,19 @@ class DashboardController extends ActionController
         }
         $pane = $this->_request->getParam('pane');
         $component = $this->_request->getParam('component');
-        $form->setOnSuccess(function (Form $form) use ($dashboard, $component, $pane) {
+        $action = $this;
+        $form->setOnSuccess(function (Form $form) use ($dashboard, $component, $pane, $action) {
             try {
                 $pane = $dashboard->getPane($pane);
                 $pane->removeComponent($component);
                 $dashboard->write();
                 Notification::success(t('Component has been removed from') . ' ' . $pane->getTitle());
                 return true;
+            }  catch (\Zend_Config_Exception $e) {
+                $action->view->error = $e;
+                $action->view->config = $dashboard->createWriter();
+                $action->render('error');
+                return false;
             } catch (ProgrammingError $e) {
                 Notification::error($e->getMessage());
                 return false;
@@ -172,13 +194,19 @@ class DashboardController extends ActionController
             );
         }
         $pane = $this->_request->getParam('pane');
-        $form->setOnSuccess(function (Form $form) use ($dashboard, $pane) {
+        $action = $this;
+        $form->setOnSuccess(function (Form $form) use ($dashboard, $pane, $action) {
             try {
                 $pane = $dashboard->getPane($pane);
                 $dashboard->removePane($pane->getTitle());
                 $dashboard->write();
                 Notification::success(t('Pane has been removed') . ': ' . $pane->getTitle());
                 return true;
+            }  catch (\Zend_Config_Exception $e) {
+                $action->view->error = $e;
+                $action->view->config = $dashboard->createWriter();
+                $action->render('error');
+                return false;
             } catch (ProgrammingError $e) {
                 Notification::error($e->getMessage());
                 return false;
diff --git a/application/views/scripts/dashboard/error.phtml b/application/views/scripts/dashboard/error.phtml
new file mode 100644
index 0000000..e5a0f39
--- /dev/null
+++ b/application/views/scripts/dashboard/error.phtml
@@ -0,0 +1,13 @@
+<div class="content">
+    <h1><?= t('Could not persist dashboard'); ?></h1>
+    <p>
+        <?= t('Please copy the following dashboard snippet to '); ?>
+        <strong><?= $this->config->getFilename(); ?>;</strong>.
+        <br>
+        <?= t('Make sure that the webserver can write to this file.'); ?>
+    </p>
+    <pre><?= (string) $this->config->render(); ?></pre>
+    <hr>
+    <h2><?= t('Error details') ?></h2>
+    <p><?= $this->error->getMessage(); ?></p>
+</div>
diff --git a/application/views/scripts/dashboard/new-component.phtml b/application/views/scripts/dashboard/new-component.phtml
index 456d14a..46c8b22 100644
--- a/application/views/scripts/dashboard/new-component.phtml
+++ b/application/views/scripts/dashboard/new-component.phtml
@@ -1,7 +1,6 @@
 <div class="controls">
     <?= $this->tabs ?>
 </div>
-
 <div class="content">
     <h1><?= t('Add Component To Dashboard'); ?></h1>
     <?= $this->form; ?>
diff --git a/library/Icinga/File/Ini/IniWriter.php b/library/Icinga/File/Ini/IniWriter.php
index cf9355b..e3b757b 100644
--- a/library/Icinga/File/Ini/IniWriter.php
+++ b/library/Icinga/File/Ini/IniWriter.php
@@ -229,4 +229,14 @@ class IniWriter extends Zend_Config_Writer_FileAbstract
 
         return $combinations;
     }
+
+    /**
+     * Getter for filename
+     * 
+     * @return string
+     */
+    public function getFilename()
+    {
+        return $this->_filename;
+    }
 }
diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php
index e663ddf..c97bdf5 100644
--- a/library/Icinga/Web/Widget/Dashboard.php
+++ b/library/Icinga/Web/Widget/Dashboard.php
@@ -86,9 +86,11 @@ class Dashboard extends AbstractWidget
     }
 
     /**
-     * Write user specific dashboards to disk
+     * Create a writer object
+     *
+     * @return IniWriter
      */
-    public function write()
+    public function createWriter()
     {
         $configFile = $this->getConfigFile();
         $output = array();
@@ -105,8 +107,15 @@ class Dashboard extends AbstractWidget
 
         $co = new ConfigObject($output);
         $config = new Config($co);
-        $writer = new IniWriter(array('config' => $config, 'filename' => $configFile));
-        $writer->write();
+        return new IniWriter(array('config' => $config, 'filename' => $configFile));
+    }
+
+    /**
+     * Write user specific dashboards to disk
+     */
+    public function write()
+    {
+        $this->createWriter()->write();
     }
 
     /**



More information about the icinga-checkins mailing list