diff options
author | Jana Peper <jana.peper@nextcloud.com> | 2024-12-17 17:00:07 +0100 |
---|---|---|
committer | janepie <49834966+janepie@users.noreply.github.com> | 2024-12-18 18:32:34 +0100 |
commit | ee31b3bbe525d1499d7e64962fd0d3521e23a358 (patch) | |
tree | 0c9c576d043f97402fef8cb5c34bcb283f994631 | |
parent | d87302c651dfb18d80856c07d7a16e9c97d68bb0 (diff) | |
download | nextcloud-server-ee31b3bbe525d1499d7e64962fd0d3521e23a358.tar.gz nextcloud-server-ee31b3bbe525d1499d7e64962fd0d3521e23a358.zip |
fix: error handling for wrong json values
Signed-off-by: Jana Peper <jana.peper@nextcloud.com>
-rw-r--r-- | apps/settings/lib/Settings/Admin/ArtificialIntelligence.php | 17 | ||||
-rw-r--r-- | core/Command/TaskProcessing/EnabledCommand.php | 23 | ||||
-rw-r--r-- | lib/private/TaskProcessing/Manager.php | 20 |
3 files changed, 46 insertions, 14 deletions
diff --git a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php index a7e5276762d..555d3c27313 100644 --- a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php +++ b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php @@ -24,6 +24,7 @@ use OCP\Translation\ITranslationProviderWithId; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; class ArtificialIntelligence implements IDelegatedSettings { public function __construct( @@ -36,6 +37,7 @@ class ArtificialIntelligence implements IDelegatedSettings { private ContainerInterface $container, private \OCP\TextToImage\IManager $text2imageManager, private \OCP\TaskProcessing\IManager $taskProcessingManager, + private LoggerInterface $logger, ) { } @@ -143,7 +145,20 @@ class ArtificialIntelligence implements IDelegatedSettings { $value = $defaultValue; $json = $this->config->getAppValue('core', $key, ''); if ($json !== '') { - $value = json_decode($json, true, flags: JSON_THROW_ON_ERROR); + try { + $value = json_decode($json, true, flags: JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + $this->logger->error('Failed to get settings. JSON Error in ' . $key, ['exception' => $e]); + if ($key === 'ai.taskprocessing_type_preferences') { + $value = []; + foreach ($taskProcessingTypeSettings as $taskTypeId => $taskTypeValue) { + $value[$taskTypeId] = false; + } + $settings[$key] = $value; + } + continue; + } + switch ($key) { case 'ai.taskprocessing_provider_preferences': case 'ai.taskprocessing_type_preferences': diff --git a/core/Command/TaskProcessing/EnabledCommand.php b/core/Command/TaskProcessing/EnabledCommand.php index 44a5cdef256..b382de12a81 100644 --- a/core/Command/TaskProcessing/EnabledCommand.php +++ b/core/Command/TaskProcessing/EnabledCommand.php @@ -41,16 +41,21 @@ class EnabledCommand extends Base { $enabled = (bool)$input->getArgument('enabled'); $taskType = $input->getArgument('task-type-id'); $json = $this->config->getAppValue('core', 'ai.taskprocessing_type_preferences'); - if ($json === '') { - $taskTypeSettings = []; - } else { - $taskTypeSettings = json_decode($json, true, flags: JSON_THROW_ON_ERROR); + try { + if ($json === '') { + $taskTypeSettings = []; + } else { + $taskTypeSettings = json_decode($json, true, flags: JSON_THROW_ON_ERROR); + } + + $taskTypeSettings[$taskType] = $enabled; + + $this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskTypeSettings)); + $this->writeArrayInOutputFormat($input, $output, $taskTypeSettings); + return 0; + } catch (\JsonException $e) { + throw new \JsonException('Error in TaskType DB entry'); } - $taskTypeSettings[$taskType] = $enabled; - - $this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskTypeSettings)); - $this->writeArrayInOutputFormat($input, $output, $taskTypeSettings); - return 0; } } diff --git a/lib/private/TaskProcessing/Manager.php b/lib/private/TaskProcessing/Manager.php index 8afc62a05d4..96cdc8c44f8 100644 --- a/lib/private/TaskProcessing/Manager.php +++ b/lib/private/TaskProcessing/Manager.php @@ -568,11 +568,23 @@ class Manager implements IManager { * @return array */ private function _getTaskTypeSettings(): array { - $json = $this->config->getAppValue('core', 'ai.taskprocessing_type_preferences', ''); - if ($json === '') { - return []; + try { + $json = $this->config->getAppValue('core', 'ai.taskprocessing_type_preferences', ''); + if ($json === '') { + return []; + } + return json_decode($json, true, flags: JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + $this->logger->error('Failed to get settings. JSON Error in ai.taskprocessing_type_preferences', ['exception' => $e]); + $taskTypeSettings = []; + $taskTypes = $this->_getTaskTypes(); + foreach ($taskTypes as $taskType) { + $taskTypeSettings[$taskType->getId()] = false; + }; + + return $taskTypeSettings; } - return json_decode($json, true, flags: JSON_THROW_ON_ERROR); + } /** |