better error message

This commit is contained in:
Emmanuel Garette 2019-11-20 08:27:33 +01:00
parent ce297ed804
commit 13c0c0e256
1 changed files with 50 additions and 29 deletions

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_ = []
lst.sort() for l in lst:
lst_ = [] if not isinstance(l, str):
for l in lst[:-1]: l = str(l)
if not isinstance(l, str): lst_.append(_(l))
l = str(l) lst__ = []
if add_quote and not l.startswith('"'): for l in lst_:
l = '"{}"'.format(l) if add_quote and not l.startswith('"'):
lst_.append(_(l)) l = '"{}"'.format(l)
last = lst[-1] lst__.append(l)
if not isinstance(last, str): lst__.sort()
last = str(_(last)) last = lst__[-1]
if add_quote and not last.startswith('"'): return ', '.join(lst__[:-1]) + _(' {} ').format(separator) + '{}'.format(last)
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 self._orig_opt: if properties == ['frozen']:
self.msg = str(_('cannot access to {0} "{1}" because "{2}" has {3} {4}' if self._orig_opt:
'').format(self._opt_type, msg = 'cannot modify the {0} "{1}" because "{2}" has {3} {4}'
self._orig_opt.impl_get_display_name(), else:
self._name, msg = 'cannot modify the {0} "{1}" because has {2} {3}'
prop_msg,
msg))
else: else:
self.msg = str(_('cannot access to {0} "{1}" because has {2} {3}' if self._orig_opt:
'').format(self._opt_type, msg = 'cannot access to {0} "{1}" because "{2}" has {3} {4}'
self._name, else:
prop_msg, msg = 'cannot access to {0} "{1}" because has {2} {3}'
msg)) if self._orig_opt:
self.msg = _(msg).format(self._opt_type,
self._orig_opt.impl_get_display_name(),
self._name,
prop_msg,
properties_msg)
else:
self.msg = _(msg).format(self._opt_type,
self._name,
prop_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}"')