fix: error handling for wrong json values

Signed-off-by: Jana Peper <jana.peper@nextcloud.com>
pull/49727/head
Jana Peper 4 months ago committed by janepie
parent d87302c651
commit ee31b3bbe5
  1. 15
      apps/settings/lib/Settings/Admin/ArtificialIntelligence.php
  2. 5
      core/Command/TaskProcessing/EnabledCommand.php
  3. 12
      lib/private/TaskProcessing/Manager.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 !== '') {
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':

@ -41,6 +41,7 @@ class EnabledCommand extends Base {
$enabled = (bool)$input->getArgument('enabled');
$taskType = $input->getArgument('task-type-id');
$json = $this->config->getAppValue('core', 'ai.taskprocessing_type_preferences');
try {
if ($json === '') {
$taskTypeSettings = [];
} else {
@ -52,5 +53,9 @@ class EnabledCommand extends Base {
$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');
}
}
}

@ -568,11 +568,23 @@ class Manager implements IManager {
* @return array
*/
private function _getTaskTypeSettings(): array {
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;
}
}
/**

Loading…
Cancel
Save