From 0065cbe998215d3e05af2a315a724c0445bcc9cf Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Mon, 23 Jun 2014 12:40:13 +0200 Subject: [PATCH] Fix PHP Fatal error "Method Icinga\File\Csv::__toString() must not throw an exception in /vagrant/test/php/library/Icinga/File/CsvTest.php on line 35" --- library/Icinga/File/Csv.php | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/library/Icinga/File/Csv.php b/library/Icinga/File/Csv.php index fb26d4c11..775ec7eb4 100644 --- a/library/Icinga/File/Csv.php +++ b/library/Icinga/File/Csv.php @@ -5,6 +5,7 @@ namespace Icinga\File; use Icinga\Data\Browsable; +use Exception; class Csv { @@ -27,20 +28,24 @@ class Csv public function __toString() { - $first = true; - $csv = ''; - foreach ($this->query->getQuery()->fetchAll() as $row) { - if ($first) { - $csv .= implode(',', array_keys((array) $row)) . "\r\n"; - $first = false; + try { + $first = true; + $csv = ''; + foreach ($this->query->getQuery()->fetchAll() as $row) { + if ($first) { + $csv .= implode(',', array_keys((array) $row)) . "\r\n"; + $first = false; + } + $out = array(); + foreach ($row as & $val) { + $out[] = '"' . $val . '"'; + } + $csv .= implode(',', $out) . "\r\n"; } - $out = array(); - foreach ($row as & $val) { - $out[] = '"' . $val . '"'; - } - $csv .= implode(',', $out) . "\r\n"; - } - return $csv; + return $csv; + } catch (Exception $e) { + return (string) $e; + } } }