From 735a4cb152b7ee5596a36f68ea0490a76eba499b Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 16 Dec 2016 15:28:15 +0100 Subject: [PATCH] Allow monitoring/Controller to handle rendering errors while serving JSON or CSV refs #13623 --- library/Icinga/File/Csv.php | 5 +++-- library/Icinga/File/Json.php | 1 + modules/monitoring/library/Monitoring/Controller.php | 8 ++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/library/Icinga/File/Csv.php b/library/Icinga/File/Csv.php index a71c36d12..580b55b8d 100644 --- a/library/Icinga/File/Csv.php +++ b/library/Icinga/File/Csv.php @@ -25,6 +25,7 @@ class Csv { $csv = new static(); $csv->query = $query; + $csv->render(); return $csv; } @@ -50,10 +51,10 @@ class Csv $first = true; foreach ($this->query as $row) { if ($first) { - $this->renderBuffer->append($this->renderRow(array_keys((array)$row))); + $this->renderBuffer->append($this->renderRow(array_keys((array) $row))); $first = false; } - $this->renderBuffer->append($this->renderRow(array_values((array)$row))); + $this->renderBuffer->append($this->renderRow(array_values((array) $row))); } } diff --git a/library/Icinga/File/Json.php b/library/Icinga/File/Json.php index f45f0f1a4..16a84b60b 100644 --- a/library/Icinga/File/Json.php +++ b/library/Icinga/File/Json.php @@ -35,6 +35,7 @@ class Json protected function __construct(Traversable $query) { $this->query = $query; + $this->render(); } /** diff --git a/modules/monitoring/library/Monitoring/Controller.php b/modules/monitoring/library/Monitoring/Controller.php index 122b15d97..6fddc6be7 100644 --- a/modules/monitoring/library/Monitoring/Controller.php +++ b/modules/monitoring/library/Monitoring/Controller.php @@ -52,6 +52,8 @@ class Controller extends IcingaWebController . ''; exit; case 'json': + $json = Json::create($query); + $response = $this->getResponse(); $response ->setHeader('Content-Type', 'application/json') @@ -65,10 +67,12 @@ class Controller extends IcingaWebController while (ob_get_level()) { ob_end_clean(); } - Json::create($query)->dump(); + $json->dump(); exit; case 'csv': + $csv = Csv::fromQuery($query); + $response = $this->getResponse(); $response ->setHeader('Content-Type', 'text/csv') @@ -82,7 +86,7 @@ class Controller extends IcingaWebController while (ob_get_level()) { ob_end_clean(); } - Csv::fromQuery($query)->dump(); + $csv->dump(); exit; }