better error message

This commit is contained in:
Emmanuel Garette 2019-11-20 08:27:33 +01:00
parent ce297ed804
commit 13c0c0e256

View file

@ -33,21 +33,19 @@ def display_list(lst, separator='and', add_quote=False):
if add_quote and not ret.startswith('"'): if add_quote and not ret.startswith('"'):
ret = '"{}"'.format(ret) ret = '"{}"'.format(ret)
return ret return ret
else:
lst.sort()
lst_ = [] lst_ = []
for l in lst[:-1]: for l in lst:
if not isinstance(l, str): if not isinstance(l, str):
l = str(l) l = str(l)
lst_.append(_(l))
lst__ = []
for l in lst_:
if add_quote and not l.startswith('"'): if add_quote and not l.startswith('"'):
l = '"{}"'.format(l) l = '"{}"'.format(l)
lst_.append(_(l)) lst__.append(l)
last = lst[-1] lst__.sort()
if not isinstance(last, str): last = lst__[-1]
last = str(_(last)) return ', '.join(lst__[:-1]) + _(' {} ').format(separator) + '{}'.format(last)
if add_quote and not last.startswith('"'):
last = '"{}"'.format(last)
return ', '.join(lst_) + _(' {} ').format(separator) + '{}'.format(last)
# Exceptions for an Option # Exceptions for an Option
@ -89,30 +87,40 @@ class PropertiesOptionError(AttributeError):
properties = list(self._settings.calc_raises_properties(self._option_bag, properties = list(self._settings.calc_raises_properties(self._option_bag,
apply_requires=False)) apply_requires=False))
for property_ in self._settings.get_calculated_properties(self._option_bag): for property_ in self._settings.get_calculated_properties(self._option_bag):
properties.append(property_.help(self._option_bag)) prop = property_.help(self._option_bag)
if prop is not None:
properties.append(prop)
if not properties: if not properties:
# if proptype == ['mandatory'] # if proptype == ['mandatory']
properties = self.proptype properties = self.proptype
only_one = len(properties) == 1 only_one = len(properties) == 1
msg = display_list(properties, add_quote=True) properties_msg = display_list(properties, add_quote=True)
if only_one: if only_one:
prop_msg = _('property') prop_msg = _('property')
else: else:
prop_msg = _('properties') prop_msg = _('properties')
if properties == ['frozen']:
if self._orig_opt: if self._orig_opt:
self.msg = str(_('cannot access to {0} "{1}" because "{2}" has {3} {4}' msg = 'cannot modify the {0} "{1}" because "{2}" has {3} {4}'
'').format(self._opt_type, else:
msg = 'cannot modify the {0} "{1}" because has {2} {3}'
else:
if self._orig_opt:
msg = 'cannot access to {0} "{1}" because "{2}" has {3} {4}'
else:
msg = 'cannot access to {0} "{1}" because has {2} {3}'
if self._orig_opt:
self.msg = _(msg).format(self._opt_type,
self._orig_opt.impl_get_display_name(), self._orig_opt.impl_get_display_name(),
self._name, self._name,
prop_msg, prop_msg,
msg)) properties_msg)
else: else:
self.msg = str(_('cannot access to {0} "{1}" because has {2} {3}' self.msg = _(msg).format(self._opt_type,
'').format(self._opt_type,
self._name, self._name,
prop_msg, prop_msg,
msg)) properties_msg)
del self._opt_type, self._name del self._opt_type, self._name
del self._settings, self._orig_opt del self._settings, self._orig_opt
return self.msg return self.msg
@ -184,6 +192,19 @@ class _CommonError:
class ValueWarning(_CommonError, UserWarning): class ValueWarning(_CommonError, UserWarning):
tmpl = _('attention, "{0}" could be an invalid {1} for "{2}"') tmpl = _('attention, "{0}" could be an invalid {1} for "{2}"')
def __init__(self, *args, **kwargs):
if len(args) == 1 and not kwargs:
self.msg = args[0]
pass
else:
super().__init__(*args, **kwargs)
self.msg = None
def __str__(self):
if self.msg is None:
return super().__str__()
return self.msg
class ValueOptionError(_CommonError, ValueError): class ValueOptionError(_CommonError, ValueError):
tmpl = _('"{0}" is an invalid {1} for "{2}"') tmpl = _('"{0}" is an invalid {1} for "{2}"')