Allow utils.Setting_ parse functions to throw detailed errors

This commit is contained in:
jesopo 2019-10-04 10:25:48 +01:00
parent 7e51165a23
commit 3466a3c43e
3 changed files with 22 additions and 4 deletions

View file

@ -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" %

View file

@ -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")

View file

@ -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):