Improve error handling for config commands

- Use self._handle_config_error() for key commands too
- Catch getting an invalid key properly
- Remove (wrong) "set: " prefix
This commit is contained in:
Florian Bruhin 2017-10-08 14:40:10 +02:00
parent 8d34d4d4f5
commit 51ea56375e
2 changed files with 23 additions and 19 deletions

View File

@ -45,7 +45,7 @@ class ConfigCommands:
try:
yield
except configexc.Error as e:
raise cmdexc.CommandError("set: {}".format(e))
raise cmdexc.CommandError(str(e))
def _print_value(self, option):
"""Print the value of the given option."""
@ -108,7 +108,8 @@ class ConfigCommands:
# self._keyconfig.get_command does this, but we also need it
# normalized for the output below
key = utils.normalize_keystr(key)
cmd = self._keyconfig.get_command(key, mode)
with self._handle_config_error():
cmd = self._keyconfig.get_command(key, mode)
if cmd is None:
message.info("{} is unbound in {} mode".format(key, mode))
else:
@ -116,10 +117,8 @@ class ConfigCommands:
key, cmd, mode))
return
try:
with self._handle_config_error():
self._keyconfig.bind(key, command, mode=mode, save_yaml=True)
except configexc.KeybindingError as e:
raise cmdexc.CommandError("bind: {}".format(e))
@cmdutils.register(instance='config-commands')
def unbind(self, key, *, mode='normal'):
@ -130,10 +129,8 @@ class ConfigCommands:
mode: A mode to unbind the key in (default: `normal`).
See `:help bindings.commands` for the available modes.
"""
try:
with self._handle_config_error():
self._keyconfig.unbind(key, mode=mode, save_yaml=True)
except configexc.KeybindingError as e:
raise cmdexc.CommandError('unbind: {}'.format(e))
@cmdutils.register(instance='config-commands', star_args_optional=True)
@cmdutils.argument('option', completion=configmodel.option)

View File

@ -114,7 +114,7 @@ class TestSet:
Should show an error.
"""
with pytest.raises(cmdexc.CommandError, match="set: No option 'foo'"):
with pytest.raises(cmdexc.CommandError, match="No option 'foo'"):
commands.set(0, 'foo', 'bar')
def test_set_invalid_value(self, commands):
@ -123,14 +123,13 @@ class TestSet:
Should show an error.
"""
with pytest.raises(cmdexc.CommandError,
match="set: Invalid value 'blah' - must be a "
"boolean!"):
match="Invalid value 'blah' - must be a boolean!"):
commands.set(0, 'auto_save.session', 'blah')
def test_set_wrong_backend(self, commands, monkeypatch):
monkeypatch.setattr(objects, 'backend', usertypes.Backend.QtWebEngine)
with pytest.raises(cmdexc.CommandError,
match="set: This setting is not available with the "
match="This setting is not available with the "
"QtWebEngine backend!"):
commands.set(0, 'content.cookies.accept', 'all')
@ -142,7 +141,7 @@ class TestSet:
See https://github.com/qutebrowser/qutebrowser/issues/1109
"""
with pytest.raises(cmdexc.CommandError,
match="set: The following arguments are required: "
match="The following arguments are required: "
"value"):
commands.set(win_id=0, option=option)
@ -151,7 +150,7 @@ class TestSet:
Should show an error.
"""
with pytest.raises(cmdexc.CommandError, match="set: No option 'foo'"):
with pytest.raises(cmdexc.CommandError, match="No option 'foo'"):
commands.set(win_id=0, option='foo?')
@ -458,14 +457,23 @@ class TestBind:
assert msg.text == expected
def test_bind_invalid_mode(self, commands):
"""Run ':bind --mode=wrongmode nop'.
"""Run ':bind --mode=wrongmode a nop'.
Should show an error.
"""
with pytest.raises(cmdexc.CommandError,
match='bind: Invalid mode wrongmode!'):
match='Invalid mode wrongmode!'):
commands.bind('a', 'nop', mode='wrongmode')
def test_bind_print_invalid_mode(self, commands):
"""Run ':bind --mode=wrongmode a'.
Should show an error.
"""
with pytest.raises(cmdexc.CommandError,
match='Invalid mode wrongmode!'):
commands.bind('a', mode='wrongmode')
@pytest.mark.parametrize('key', ['a', 'b', '<Ctrl-X>'])
def test_bind_duplicate(self, commands, config_stub, key_config_stub, key):
"""Run ':bind' with a key which already has been bound.'.
@ -521,9 +529,8 @@ class TestBind:
assert yaml_bindings[normalized] is None
@pytest.mark.parametrize('key, mode, expected', [
('foobar', 'normal',
"unbind: Can't find binding 'foobar' in normal mode"),
('x', 'wrongmode', "unbind: Invalid mode wrongmode!"),
('foobar', 'normal', "Can't find binding 'foobar' in normal mode"),
('x', 'wrongmode', "Invalid mode wrongmode!"),
])
def test_unbind_invalid(self, commands, key, mode, expected):
"""Run ':unbind foobar' / ':unbind x wrongmode'.