Add error handling for parsing patterns from YAML

This commit is contained in:
Florian Bruhin 2018-02-20 22:29:21 +01:00
parent 46aeb25e7e
commit 17b235b523
2 changed files with 28 additions and 2 deletions

View File

@ -201,17 +201,37 @@ class YamlConfig(QObject):
def _build_values(self, settings):
"""Build up self._values from the values in the given dict."""
errors = []
for name, yaml_values in settings.items():
if not isinstance(yaml_values, dict):
errors.append(configexc.ConfigErrorDesc(
"While parsing {!r}".format(name), "value is not a dict"))
continue
values = configutils.Values(configdata.DATA[name])
if 'global' in yaml_values:
values.add(yaml_values.pop('global'))
# FIXME:conf what if yaml_values is not a dict...
for pattern, value in yaml_values.items():
values.add(value, pattern)
if not isinstance(pattern, str):
errors.append(configexc.ConfigErrorDesc(
"While parsing {!r}".format(name),
"pattern is not of type string"))
continue
try:
urlpattern = urlmatch.UrlPattern(pattern)
except urlmatch.ParseError as e:
errors.append(configexc.ConfigErrorDesc(
"While parsing pattern {!r} for {!r}"
.format(pattern, name), e))
continue
values.add(value, urlpattern)
self._values[name] = values
if errors:
raise configexc.ConfigFileErrors('autoconfig.yml', errors)
def _handle_migrations(self, settings):
"""Migrate older configs to the newest format."""
# Simple renamed/deleted options

View File

@ -297,6 +297,12 @@ class TestYaml:
('settings: {}', 'While loading data',
"Toplevel object does not contain 'config_version' key"),
('42', 'While loading data', "Toplevel object is not a dict"),
('settings: {"content.images": 42}\nconfig_version: 2',
"While parsing 'content.images'", "value is not a dict"),
('settings: {"content.images": {"https://": true}}\nconfig_version: 2',
"While parsing pattern 'https://' for 'content.images'", "Pattern without host"),
('settings: {"content.images": {true: true}}\nconfig_version: 2',
"While parsing 'content.images'", "pattern is not of type string"),
])
def test_invalid(self, yaml, autoconfig, line, text, exception):
autoconfig.write_raw(line)