better message error

This commit is contained in:
Emmanuel Garette 2019-07-26 08:51:19 +02:00
parent a33c7ed41e
commit 35ef218c9c
3 changed files with 27 additions and 14 deletions

View File

@ -70,9 +70,15 @@ class DomainnameOption(IPOption):
else:
min_time = 1
regexp = r'((?!-)[a-z0-9-]{{{1},{0}}}\.){{{1},}}[a-z0-9-]{{1,{0}}}'.format(self._get_len(type_), min_time)
msg = _('only lowercase, number, "-" and "." are characters are allowed')
msg_warning = _('only lowercase, number, "-" and "." are characters are recommanded')
else:
regexp = r'((?!-)[a-z0-9-]{{1,{0}}})'.format(self._get_len(type_))
msg = _('only lowercase, number and - are characters are allowed')
msg_warning = _('only lowercase, number and "-" are characters are recommanded')
if allow_ip:
msg = _('could be a IP, otherwise {}').format(msg)
msg_warning = _('could be a IP, otherwise {}').format(msg_warning)
if not cidr:
regexp = r'^(?:{0}|(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){{3}}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))$'.format(regexp)
else:
@ -80,6 +86,8 @@ class DomainnameOption(IPOption):
else:
regexp = r'^{0}$'.format(regexp)
extra['_domain_re'] = re.compile(regexp)
extra['_domain_re_message'] = msg
extra['_domain_re_message_warning'] = msg_warning
extra['_has_upper'] = re.compile('[A-Z]')
super().__init__(name,
@ -141,6 +149,6 @@ class DomainnameOption(IPOption):
raise ValueError(_('some characters are uppercase'))
if not self.impl_get_extra('_domain_re').search(value):
if warnings_only:
raise ValueError(_('some characters may cause problems'))
raise ValueError(self.impl_get_extra('_domain_re_message_warning'))
else:
raise ValueError()
raise ValueError(self.impl_get_extra('_domain_re_message'))

View File

@ -144,9 +144,8 @@ class IPOption(StrOption):
ip, network = vals
network_obj = ip_network(network)
if ip_interface(ip) not in network_obj:
msg = _('"{0}" is not in network "{1}" ("{2}")')
raise ValueError(msg.format(ip,
network,
msg = _('IP not in network "{0}" ("{1}")')
raise ValueError(msg.format(network,
opts[1].impl_get_display_name()))
# test if ip is not network/broadcast IP
netmask = NetmaskOption(self.impl_getname(),
@ -166,12 +165,18 @@ class IPOption(StrOption):
ip, network, netmask = vals
if ip_interface(ip) not in ip_network('{0}/{1}'.format(network,
netmask)):
msg = _('"{4}" is not in network "{0}"/"{1}" ("{2}"/"{3}")')
raise ValueError(msg.format(network,
netmask,
if current_opt == opts[0]:
msg = _('IP not in network "{2}"/"{4}" ("{3}"/"{5}")')
elif current_opt == opts[1]:
msg = _('the network doest not match with IP "{0}" ("{1}") and network "{4}" ("{5}")')
else:
msg = _('the netmask does not match with IP "{0}" ("{1}") and broadcast "{2}" ("{3}")')
raise ValueError(msg.format(ip,
opts[0].impl_get_display_name(),
network,
opts[1].impl_get_display_name(),
opts[2].impl_get_display_name(),
ip))
netmask,
opts[2].impl_get_display_name()))
# test if ip is not network/broadcast IP
opts[2]._cons_ip_netmask(current_opt,
(opts[2], opts[0]),

View File

@ -66,11 +66,11 @@ class NetmaskOption(StrOption):
ip_network('{0}/{1}'.format(val_network, val_netmask))
except ValueError:
if current_opt == opt_network:
raise ValueError(_('with netmask "{0}" ("{1}")').format(val_netmask,
opt_netmask.impl_get_display_name()))
raise ValueError(_('the netmask "{0}" ("{1}") does not match').format(val_netmask,
opt_netmask.impl_get_display_name()))
else:
raise ValueError(_('with network "{0}" ("{1}")').format(val_network,
opt_network.impl_get_display_name()))
raise ValueError(_('the network "{0}" ("{1}") does not match').format(val_network,
opt_network.impl_get_display_name()))
def _cons_ip_netmask(self,
current_opt: Option,