Allow utils.Setting_ parse functions to throw detailed errors
This commit is contained in:
parent
7e51165a23
commit
3466a3c43e
3 changed files with 22 additions and 4 deletions
|
@ -7,7 +7,8 @@ import enum
|
|||
from src import ModuleManager, utils
|
||||
|
||||
class ConfigInvalidValue(Exception):
|
||||
pass
|
||||
def __init__(self, message: str=None):
|
||||
self.message = message
|
||||
class ConfigSettingInexistent(Exception):
|
||||
pass
|
||||
|
||||
|
@ -113,7 +114,11 @@ class Module(ModuleManager.BaseModule):
|
|||
|
||||
def _config(self, export_settings, target, setting, value=None):
|
||||
if not value == None:
|
||||
validated_value = export_settings[setting].parse(value)
|
||||
try:
|
||||
validated_value = export_settings[setting].parse(value)
|
||||
except utils.SettingParseException as e:
|
||||
raise ConfigInvalidValue(str(e))
|
||||
|
||||
if not validated_value == None:
|
||||
target.set_setting(setting, validated_value)
|
||||
return ConfigResult(ConfigResults.Changed, validated_value)
|
||||
|
@ -199,7 +204,10 @@ class Module(ModuleManager.BaseModule):
|
|||
|
||||
try:
|
||||
result = self._config(export_settings, target, setting, value)
|
||||
except ConfigInvalidValue:
|
||||
except ConfigInvalidValue as e:
|
||||
if not e.message == None:
|
||||
raise utils.EventError("Invalid value: %s" % e.message)
|
||||
|
||||
example = export_settings[setting].get_example()
|
||||
if not example == None:
|
||||
raise utils.EventError("Invalid value. %s" %
|
||||
|
|
|
@ -12,10 +12,17 @@ USERPASS_MECHANISMS = [
|
|||
"SCRAM-SHA-1",
|
||||
"PLAIN"
|
||||
]
|
||||
ALL_MECHANISMS = USERPASS_MECHANISMS+["EXTERNAL"]
|
||||
|
||||
def _parse(value):
|
||||
mechanism, _, arguments = value.partition(" ")
|
||||
return {"mechanism": mechanism.upper(), "args": arguments}
|
||||
mechanism = mechanism.upper()
|
||||
|
||||
if mechanism in ALL_MECHANISMS:
|
||||
return {"mechanism": mechanism.upper(), "args": arguments}
|
||||
else:
|
||||
raise utils.SettingParseException("Unknown SASL mechanism '%s'"
|
||||
% mechanism)
|
||||
|
||||
HARDFAIL = utils.BoolSetting("sasl-hard-fail",
|
||||
"Set whether a SASL failure should cause a disconnect")
|
||||
|
|
|
@ -264,6 +264,9 @@ def is_ip(s: str) -> bool:
|
|||
def is_main_thread() -> bool:
|
||||
return threading.current_thread() is threading.main_thread()
|
||||
|
||||
class SettingParseException(Exception):
|
||||
pass
|
||||
|
||||
class Setting(object):
|
||||
example: typing.Optional[str] = None
|
||||
def __init__(self, name: str, help: str=None, example: str=None):
|
||||
|
|
Loading…
Reference in a new issue