From dca72df0fd870475be2c820fa4602026c0659ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 25 Mar 2024 16:22:29 +0100 Subject: [PATCH] fix: Catch exception from LogIteratorFactory, throw a clean error when log_type is not file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/Log/LogIteratorFactory.php | 16 +++++++++------- lib/SetupChecks/LogErrors.php | 8 +++++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/Log/LogIteratorFactory.php b/lib/Log/LogIteratorFactory.php index 21569ee7..a0ce6735 100644 --- a/lib/Log/LogIteratorFactory.php +++ b/lib/Log/LogIteratorFactory.php @@ -28,12 +28,10 @@ use OCP\Log\ILogFactory; class LogIteratorFactory { - private $config; - private $logFactory; - - public function __construct(IConfig $config, ILogFactory $logFactory) { - $this->config = $config; - $this->logFactory = $logFactory; + public function __construct( + private IConfig $config, + private ILogFactory $logFactory, + ) { } /** @@ -41,9 +39,13 @@ public function __construct(IConfig $config, ILogFactory $logFactory) { * @param int[] $levels Array of levels to show * @throws \Exception */ - public function getLogIterator(array $levels) { + public function getLogIterator(array $levels): \Iterator { $dateFormat = $this->config->getSystemValue('logdateformat', \DateTime::ATOM); $timezone = $this->config->getSystemValue('logtimezone', 'UTC'); + $logType = $this->config->getSystemValue('log_type', 'file'); + if ($logType !== 'file') { + throw new \Exception('Logreader application only supports "file" log_type'); + } $log = $this->logFactory->get('file'); if ($log instanceof IFileBased) { $handle = fopen($log->getLogFilePath(), 'rb'); diff --git a/lib/SetupChecks/LogErrors.php b/lib/SetupChecks/LogErrors.php index 6681ebac..0072e3ec 100644 --- a/lib/SetupChecks/LogErrors.php +++ b/lib/SetupChecks/LogErrors.php @@ -54,7 +54,13 @@ public function getCategory(): string { } public function run(): SetupResult { - $logIterator = $this->logIteratorFactory->getLogIterator([self::LEVEL_WARNING,self::LEVEL_ERROR,self::LEVEL_FATAL]); + try { + $logIterator = $this->logIteratorFactory->getLogIterator([self::LEVEL_WARNING,self::LEVEL_ERROR,self::LEVEL_FATAL]); + } catch (\Exception $e) { + return SetupResult::error( + $this->l10n->t('Failed to get an iterator for log entries: %s', [$e->getMessage()]) + ); + } $count = [ self::LEVEL_WARNING => 0, self::LEVEL_ERROR => 0,