From 35ef218c9cc86037fe6bd6aa9e2a6b45d6e7a375 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 26 Jul 2019 08:51:19 +0200 Subject: [PATCH] better message error --- tiramisu/option/domainnameoption.py | 12 ++++++++++-- tiramisu/option/ipoption.py | 21 +++++++++++++-------- tiramisu/option/netmaskoption.py | 8 ++++---- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/tiramisu/option/domainnameoption.py b/tiramisu/option/domainnameoption.py index 487d3ed..304efc6 100644 --- a/tiramisu/option/domainnameoption.py +++ b/tiramisu/option/domainnameoption.py @@ -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')) diff --git a/tiramisu/option/ipoption.py b/tiramisu/option/ipoption.py index 4775493..fe72f34 100644 --- a/tiramisu/option/ipoption.py +++ b/tiramisu/option/ipoption.py @@ -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]), diff --git a/tiramisu/option/netmaskoption.py b/tiramisu/option/netmaskoption.py index 3bdd0ca..c850681 100644 --- a/tiramisu/option/netmaskoption.py +++ b/tiramisu/option/netmaskoption.py @@ -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,