Skip to content

Commit

Permalink
fix(LogErrors): Handle unsupported log_type gracefully
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Dec 18, 2024
1 parent 61dab73 commit 9132fc0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/Exception/UnsupportedLogTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\LogReader\Exception;

class UnsupportedLogTypeException extends \Exception {
public function __construct() {
parent::__construct('Logreader application only supports "file" log_type');
}
}
3 changes: 2 additions & 1 deletion lib/Log/LogIteratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\LogReader\Log;

use OCA\LogReader\Exception\UnsupportedLogTypeException;
use OCP\IConfig;
use OCP\Log\IFileBased;
use OCP\Log\ILogFactory;
Expand All @@ -29,7 +30,7 @@ public function getLogIterator(array $levels): \Iterator {
$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');
throw new UnsupportedLogTypeException();
}
$log = $this->logFactory->get('file');
if ($log instanceof IFileBased) {
Expand Down
5 changes: 5 additions & 0 deletions lib/SetupChecks/LogErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace OCA\LogReader\SetupChecks;

use OCA\LogReader\Exception\UnsupportedLogTypeException;
use OCA\LogReader\Log\LogIteratorFactory;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
Expand Down Expand Up @@ -40,6 +41,10 @@ public function run(): SetupResult {
try {
$logIterator = $this->logIteratorFactory->getLogIterator([self::LEVEL_WARNING,self::LEVEL_ERROR,self::LEVEL_FATAL]);
} catch (\Exception $e) {
if ($e instanceof UnsupportedLogTypeException) {
return SetupResult::info($e->getMessage());
}

return SetupResult::error(
$this->l10n->t('Failed to get an iterator for log entries: %s', [$e->getMessage()])
);
Expand Down

0 comments on commit 9132fc0

Please sign in to comment.