Show nicer errors when trying to set deleted/renamed options

This commit is contained in:
Florian Bruhin 2017-10-11 08:00:38 +02:00
parent 0cd5d4300b
commit 2a3369e2fe
4 changed files with 40 additions and 6 deletions

View File

@ -272,7 +272,11 @@ class Config(QObject):
try:
return configdata.DATA[name]
except KeyError:
raise configexc.NoOptionError(name) from None
deleted = name in configdata.MIGRATIONS.deleted
renamed = configdata.MIGRATIONS.renamed.get(name)
exception = configexc.NoOptionError(
name, deleted=deleted, renamed=renamed)
raise exception from None
def get(self, name):
"""Get the given setting converted for Python code."""

View File

@ -63,8 +63,16 @@ class NoOptionError(Error):
"""Raised when an option was not found."""
def __init__(self, option):
super().__init__("No option {!r}".format(option))
def __init__(self, option, *, deleted=False, renamed=None):
if deleted:
assert renamed is None
suffix = ' (this option was removed from qutebrowser)'
elif renamed is not None:
suffix = ' (this option was renamed to {!r})'.format(renamed)
else:
suffix = ''
super().__init__("No option {!r}{}".format(option, suffix))
self.option = option

View File

@ -32,10 +32,20 @@ def test_validation_error():
assert str(e) == "Invalid value 'val' - msg"
def test_no_option_error():
e = configexc.NoOptionError('opt')
@pytest.mark.parametrize('deleted, renamed, expected', [
(False, None, "No option 'opt'"),
(True, None, "No option 'opt' (this option was removed from qutebrowser)"),
(False, 'new', "No option 'opt' (this option was renamed to 'new')"),
])
def test_no_option_error(deleted, renamed, expected):
e = configexc.NoOptionError('opt', deleted=deleted, renamed=renamed)
assert e.option == 'opt'
assert str(e) == "No option 'opt'"
assert str(e) == expected
def test_no_option_error_clash():
with pytest.raises(AssertionError):
e = configexc.NoOptionError('opt', deleted=True, renamed='foo')
def test_backend_error():

View File

@ -534,6 +534,18 @@ class TestConfigPy:
assert str(error.exception) == "No option 'foo'"
assert error.traceback is None
def test_renamed_option_error(self, confpy, monkeypatch):
"""Setting an option which has been renamed should show a hint."""
monkeypatch.setattr(configdata.MIGRATIONS, 'renamed',
{'qt_args': 'qt.args'})
confpy.write('c.qt_args = ["foo"]')
error = confpy.read(error=True)
assert isinstance(error.exception, configexc.NoOptionError)
expected = ("No option 'qt_args' (this option was renamed to "
"'qt.args')")
assert str(error.exception) == expected
def test_multiple_errors(self, confpy):
confpy.write("c.foo = 42", "config.set('foo', 42)", "1/0")