Add error handling for parsing patterns from YAML
This commit is contained in:
parent
46aeb25e7e
commit
17b235b523
@ -201,17 +201,37 @@ class YamlConfig(QObject):
|
|||||||
|
|
||||||
def _build_values(self, settings):
|
def _build_values(self, settings):
|
||||||
"""Build up self._values from the values in the given dict."""
|
"""Build up self._values from the values in the given dict."""
|
||||||
|
errors = []
|
||||||
for name, yaml_values in settings.items():
|
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])
|
values = configutils.Values(configdata.DATA[name])
|
||||||
if 'global' in yaml_values:
|
if 'global' in yaml_values:
|
||||||
values.add(yaml_values.pop('global'))
|
values.add(yaml_values.pop('global'))
|
||||||
|
|
||||||
# FIXME:conf what if yaml_values is not a dict...
|
|
||||||
for pattern, value in yaml_values.items():
|
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
|
self._values[name] = values
|
||||||
|
|
||||||
|
if errors:
|
||||||
|
raise configexc.ConfigFileErrors('autoconfig.yml', errors)
|
||||||
|
|
||||||
def _handle_migrations(self, settings):
|
def _handle_migrations(self, settings):
|
||||||
"""Migrate older configs to the newest format."""
|
"""Migrate older configs to the newest format."""
|
||||||
# Simple renamed/deleted options
|
# Simple renamed/deleted options
|
||||||
|
@ -297,6 +297,12 @@ class TestYaml:
|
|||||||
('settings: {}', 'While loading data',
|
('settings: {}', 'While loading data',
|
||||||
"Toplevel object does not contain 'config_version' key"),
|
"Toplevel object does not contain 'config_version' key"),
|
||||||
('42', 'While loading data', "Toplevel object is not a dict"),
|
('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):
|
def test_invalid(self, yaml, autoconfig, line, text, exception):
|
||||||
autoconfig.write_raw(line)
|
autoconfig.write_raw(line)
|
||||||
|
Loading…
Reference in New Issue
Block a user