mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
fix: error handling for wrong json values
Signed-off-by: Jana Peper <jana.peper@nextcloud.com>
This commit is contained in:
parent
d87302c651
commit
ee31b3bbe5
3 changed files with 46 additions and 14 deletions
|
|
@ -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':
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue