Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Catch exception from LogIteratorFactory, throw a clean error when log_type is not file #1195

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading