Skip to content

Commit

Permalink
Merge pull request #1197 from nextcloud/backport/1195/stable28
Browse files Browse the repository at this point in the history
[stable28] fix: Catch exception from LogIteratorFactory, throw a clean error when log_type is not file
  • Loading branch information
susnux committed Mar 26, 2024
2 parents b63676b + dca72df commit 6b06e3d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
16 changes: 9 additions & 7 deletions lib/Log/LogIteratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@
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,
) {
}

/**
* @return \Iterator
* @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');
Expand Down
8 changes: 7 additions & 1 deletion lib/SetupChecks/LogErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6b06e3d

Please sign in to comment.