Improve :set error messages.

This commit is contained in:
Florian Bruhin 2015-11-10 22:09:36 +01:00
parent 54e2cea460
commit d99f9a3a20
2 changed files with 25 additions and 14 deletions

View File

@ -29,6 +29,7 @@ import sys
import os.path
import functools
import configparser
import contextlib
import collections
import collections.abc
@ -666,6 +667,18 @@ class ConfigManager(QObject):
newval = val.typ.transform(newval)
return newval
@contextlib.contextmanager
def _handle_config_error(self):
"""Catch errors in set_command and raise CommandError."""
try:
yield
except (configexc.NoOptionError, configexc.NoSectionError,
configexc.ValidationError) as e:
raise cmdexc.CommandError("set: {}".format(e))
except (configexc.Error, configparser.Error) as e:
raise cmdexc.CommandError("set: {} - {}".format(
e.__class__.__name__, e))
@cmdutils.register(name='set', instance='config', win_id='win_id',
completion=[Completion.section, Completion.option,
Completion.value])
@ -703,7 +716,7 @@ class ConfigManager(QObject):
option = option[:-1]
print_ = True
else:
try:
with self._handle_config_error():
if option.endswith('!') and option != '!' and value is None:
option = option[:-1]
val = self.get(section_, option)
@ -719,16 +732,10 @@ class ConfigManager(QObject):
else:
raise cmdexc.CommandError("set: The following arguments "
"are required: value")
except (configexc.Error, configparser.Error) as e:
raise cmdexc.CommandError("set: {} - {}".format(
e.__class__.__name__, e))
if print_:
try:
with self._handle_config_error():
val = self.get(section_, option, transformed=False)
except configexc.Error as e:
raise cmdexc.CommandError("set: {} - {}".format(
e.__class__.__name__, e))
message.info(win_id, "{} {} = {}".format(
section_, option, val), immediately=True)

View File

@ -17,11 +17,11 @@ Feature: Setting settings.
Scenario: Invalid section
When I run :set blah blub foo
Then the error "set: NoSectionError - Section 'blah' does not exist!" should be shown.
Then the error "set: Section 'blah' does not exist!" should be shown.
Scenario: Invalid option
When I run :set general blub foo
Then the error "set: NoOptionError - No option 'blub' in section 'general'" should be shown.
Then the error "set: No option 'blub' in section 'general'" should be shown.
Scenario: Toggling an option
When I run :set general auto-save-config false
@ -46,6 +46,10 @@ Feature: Setting settings.
And I run :set -p general auto-save-config!
Then the message "general auto-save-config = True" should be shown.
Scenario: Setting an invalid value
When I run :set general auto-save-config blah
Then the error "set: Invalid value 'blah' - must be a boolean!" should be shown.
Scenario: Setting a temporary option
# We don't actually check if the option is temporary as this isn't easy
# to check.
@ -74,11 +78,11 @@ Feature: Setting settings.
Scenario: Invalid option with ? (issue 1109)
When I run :set general foo?
Then the error "set: NoOptionError - No option 'foo' in section 'general'" should be shown.
Then the error "set: No option 'foo' in section 'general'" should be shown.
Scenario: Invalid section/option with ? (issue 1109)
When I run :set blah foo ?
Then the error "set: NoSectionError - Section 'blah' does not exist!" should be shown.
Then the error "set: Section 'blah' does not exist!" should be shown.
Scenario: Empty option with !
When I run :set general !
@ -90,8 +94,8 @@ Feature: Setting settings.
Scenario: Invalid option with !
When I run :set general foo!
Then the error "set: NoOptionError - No option 'foo' in section 'general'" should be shown.
Then the error "set: No option 'foo' in section 'general'" should be shown.
Scenario: Invalid section/option with !
When I run :set blah foo !
Then the error "set: NoSectionError - Section 'blah' does not exist!" should be shown.
Then the error "set: Section 'blah' does not exist!" should be shown.