Print nice error messages on wrong setting values

This commit is contained in:
Florian Bruhin 2014-04-10 12:20:15 +02:00
parent c05e66a9b9
commit 32ce8e6772
2 changed files with 16 additions and 11 deletions

View File

@ -34,6 +34,7 @@ from configparser import ConfigParser, ExtendedInterpolation
import qutebrowser.config.configdata as configdata import qutebrowser.config.configdata as configdata
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
import qutebrowser.utils.message as message import qutebrowser.utils.message as message
from qutebrowser.config.conftypes import ValidationError
config = None config = None
state = None state = None
@ -249,7 +250,7 @@ class Config:
# FIXME completion for values # FIXME completion for values
try: try:
self.set(section, option, value) self.set(section, option, value)
except (NoOptionError, NoSectionError) as e: except (NoOptionError, NoSectionError, ValidationError) as e:
message.error("set: {} - {}".format(e.__class__.__name__, e)) message.error("set: {} - {}".format(e.__class__.__name__, e))
def set(self, section, option, value): def set(self, section, option, value):

View File

@ -22,7 +22,8 @@ class ValidationError(ValueError):
"""Exception raised when a value for a config type was invalid.""" """Exception raised when a value for a config type was invalid."""
pass def __init__(self, value, msg):
super().__init__('Invalid value "{}" - {}'.format(value, msg))
class ValidValues: class ValidValues:
@ -99,6 +100,7 @@ class SettingValue:
@value.setter @value.setter
def value(self, val): def value(self, val):
"""Set the currently valid value.""" """Set the currently valid value."""
self.typ.validate(val)
self.rawvalue = val self.rawvalue = val
@ -149,7 +151,8 @@ class BaseType:
""" """
if self.valid_values is not None: if self.valid_values is not None:
if value not in self.valid_values: if value not in self.valid_values:
raise ValidationError raise ValidationError(value, "valid values: {}".format(
','.join(self.valid_values)))
else: else:
return return
else: else:
@ -182,7 +185,7 @@ class Bool(BaseType):
def validate(self, value): def validate(self, value):
if value.lower() not in self._BOOLEAN_STATES: if value.lower() not in self._BOOLEAN_STATES:
raise ValidationError raise ValidationError(value, "must be a boolean!")
class Int(BaseType): class Int(BaseType):
@ -198,7 +201,7 @@ class Int(BaseType):
try: try:
int(value) int(value)
except ValueError: except ValueError:
raise ValidationError raise ValidationError(value, "must be an integer!")
class List(BaseType): class List(BaseType):
@ -228,7 +231,7 @@ class IntList(List):
try: try:
self.transform(value) self.transform(value)
except ValueError: except ValueError:
raise ValidationError raise ValidationError(value, "must be a list of integers!")
class PercOrInt(BaseType): class PercOrInt(BaseType):
@ -240,18 +243,19 @@ class PercOrInt(BaseType):
try: try:
intval = int(value.rstrip('%')) intval = int(value.rstrip('%'))
except ValueError: except ValueError:
raise ValidationError raise ValidationError(value, "invalid percentage!")
else: else:
if not 0 <= intval <= 100: if not 0 <= intval <= 100:
raise ValidationError raise ValidationError(value, "percentage needs to be >= 0 "
"and <= 100!")
else: else:
try: try:
intval = int(value) intval = int(value)
except ValueError: except ValueError:
raise ValidationError raise ValidationError(value, "must be integer or percentage!")
else: else:
if intval < 0: if intval < 0:
raise ValidationError raise ValidationError(value, "must be >= 0")
class Command(BaseType): class Command(BaseType):
@ -271,7 +275,7 @@ class Command(BaseType):
#try: #try:
# cp.parse(value) # cp.parse(value)
#except NoSuchCommandError: #except NoSuchCommandError:
# raise ValidationError # raise ValidationError(value, "must be a valid command!")
pass pass