mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
config: Tweak logic to return a validation error when custom validation fails
This commit is contained in:
parent
66fcea72e5
commit
0789eca4e0
3 changed files with 29 additions and 12 deletions
|
@ -242,8 +242,20 @@ _ynh_app_config_validate() {
|
||||||
fi
|
fi
|
||||||
if [ -n "$result" ]
|
if [ -n "$result" ]
|
||||||
then
|
then
|
||||||
local key="YNH_ERROR_${short_setting}"
|
#
|
||||||
ynh_return "$key: \"$result\""
|
# Return a yaml such as:
|
||||||
|
#
|
||||||
|
# validation_errors:
|
||||||
|
# some_key: "An error message"
|
||||||
|
# some_other_key: "Another error message"
|
||||||
|
#
|
||||||
|
# We use changes_validated to know if this is
|
||||||
|
# the first validation error
|
||||||
|
if [[ "$changes_validated" == true ]]
|
||||||
|
then
|
||||||
|
ynh_return "validation_errors:"
|
||||||
|
fi
|
||||||
|
ynh_return " ${short_setting}: \"$result\""
|
||||||
changes_validated=false
|
changes_validated=false
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
|
@ -1695,9 +1695,7 @@ def app_config_set(
|
||||||
|
|
||||||
Question.operation_logger = operation_logger
|
Question.operation_logger = operation_logger
|
||||||
|
|
||||||
result = config_.set(key, value, args, args_file, operation_logger=operation_logger)
|
return config_.set(key, value, args, args_file, operation_logger=operation_logger)
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
class AppConfigPanel(ConfigPanel):
|
class AppConfigPanel(ConfigPanel):
|
||||||
|
@ -1715,7 +1713,18 @@ class AppConfigPanel(ConfigPanel):
|
||||||
|
|
||||||
def _apply(self):
|
def _apply(self):
|
||||||
env = {key: str(value) for key, value in self.new_values.items()}
|
env = {key: str(value) for key, value in self.new_values.items()}
|
||||||
self.errors = self._call_config_script("apply", env=env)
|
return_content = self._call_config_script("apply", env=env)
|
||||||
|
|
||||||
|
# If the script returned validation error
|
||||||
|
# raise a ValidationError exception using
|
||||||
|
# the first key
|
||||||
|
if return_content:
|
||||||
|
for key, message in return_content.get("validation_errors").items():
|
||||||
|
raise YunohostValidationError(
|
||||||
|
"app_argument_invalid",
|
||||||
|
name=key,
|
||||||
|
error=message,
|
||||||
|
)
|
||||||
|
|
||||||
def _call_config_script(self, action, env={}):
|
def _call_config_script(self, action, env={}):
|
||||||
from yunohost.hook import hook_exec
|
from yunohost.hook import hook_exec
|
||||||
|
|
|
@ -135,6 +135,8 @@ class ConfigPanel:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._apply()
|
self._apply()
|
||||||
|
except YunohostError:
|
||||||
|
raise
|
||||||
# Script got manually interrupted ...
|
# Script got manually interrupted ...
|
||||||
# N.B. : KeyboardInterrupt does not inherit from Exception
|
# N.B. : KeyboardInterrupt does not inherit from Exception
|
||||||
except (KeyboardInterrupt, EOFError):
|
except (KeyboardInterrupt, EOFError):
|
||||||
|
@ -152,16 +154,10 @@ class ConfigPanel:
|
||||||
# Delete files uploaded from API
|
# Delete files uploaded from API
|
||||||
FileQuestion.clean_upload_dirs()
|
FileQuestion.clean_upload_dirs()
|
||||||
|
|
||||||
if self.errors:
|
|
||||||
return {
|
|
||||||
"errors": self.errors,
|
|
||||||
}
|
|
||||||
|
|
||||||
self._reload_services()
|
self._reload_services()
|
||||||
|
|
||||||
logger.success("Config updated as expected")
|
logger.success("Config updated as expected")
|
||||||
operation_logger.success()
|
operation_logger.success()
|
||||||
return {}
|
|
||||||
|
|
||||||
def _get_toml(self):
|
def _get_toml(self):
|
||||||
return read_toml(self.config_path)
|
return read_toml(self.config_path)
|
||||||
|
|
Loading…
Add table
Reference in a new issue