Exceptions constraints improved

Exceptions were too general, potentially swallowing
unexpected issues and hiding them from the view of maintainers.

All of the problematic except blocks were refactored
to only consider exceptions directly related to their try blocks.

Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I48b237d1a8f3bab3b0817db5edb9aade0a941e42
This commit is contained in:
Jiri Podivin 2021-04-15 13:32:16 +02:00
parent 36b5c707e6
commit 64994c781b
2 changed files with 9 additions and 9 deletions

View File

@ -76,7 +76,7 @@ def open_network_environment_files(netenv_path, template_files):
try: try:
network_data = yaml_safe_load(template_files[netenv_path]) network_data = yaml_safe_load(template_files[netenv_path])
except Exception as e: except IOError as e:
return ({}, {}, ["Can't open network environment file '{}': {}" return ({}, {}, ["Can't open network environment file '{}': {}"
.format(netenv_path, e)]) .format(netenv_path, e)])
nic_configs = [] nic_configs = []
@ -89,7 +89,7 @@ def open_network_environment_files(netenv_path, template_files):
nic_configs.append(( nic_configs.append((
nic_name, nic_config_path, nic_name, nic_config_path,
yaml_safe_load(template_files[nic_config_path]))) yaml_safe_load(template_files[nic_config_path])))
except Exception as e: except IOError as e:
errors.append( errors.append(
"Can't open the resource '{}' reference file '{}': {}" "Can't open the resource '{}' reference file '{}': {}"
.format(nic_name, nic_config_path, e)) .format(nic_name, nic_config_path, e))
@ -254,7 +254,7 @@ def check_allocation_pools_pairing(filedata, pools):
pool_objs.append(netaddr.IPRange( pool_objs.append(netaddr.IPRange(
netaddr.IPAddress(dict_range['start']), netaddr.IPAddress(dict_range['start']),
netaddr.IPAddress(dict_range['end']))) netaddr.IPAddress(dict_range['end'])))
except Exception: except (ValueError, TypeError, KeyError, netaddr.AddrFormatError):
errors.append("Invalid format of the IP range in {}: {}" errors.append("Invalid format of the IP range in {}: {}"
.format(poolitem, dict_range)) .format(poolitem, dict_range))
continue continue
@ -268,7 +268,7 @@ def check_allocation_pools_pairing(filedata, pools):
errors.append('The {} CIDR is not specified for {}.' errors.append('The {} CIDR is not specified for {}.'
.format(subnet_item, poolitem)) .format(subnet_item, poolitem))
continue continue
except Exception: except (netaddr.AddrFormatError, ValueError):
errors.append('Invalid IP network: {}'.format(network)) errors.append('Invalid IP network: {}'.format(network))
continue continue
@ -323,7 +323,7 @@ def check_static_ip_pool_collision(static_ips, pools):
try: try:
ip_range = netaddr.IPRange(allocation_range['start'], ip_range = netaddr.IPRange(allocation_range['start'],
allocation_range['end']) allocation_range['end'])
except Exception: except (netaddr.AddrFormatError, TypeError, KeyError):
errors.append("Invalid format of the IP range in {}: {}" errors.append("Invalid format of the IP range in {}: {}"
.format(pool_name, allocation_range)) .format(pool_name, allocation_range))
continue continue
@ -398,7 +398,7 @@ def check_static_ip_in_cidr(networks, static_ips):
for name, cidr in six.iteritems(networks): for name, cidr in six.iteritems(networks):
try: try:
network_ranges[name] = netaddr.IPNetwork(cidr) network_ranges[name] = netaddr.IPNetwork(cidr)
except Exception: except (netaddr.AddrFormatError, ValueError):
errors.append("Network '{}' has an invalid CIDR: '{}'" errors.append("Network '{}' has an invalid CIDR: '{}'"
.format(name, cidr)) .format(name, cidr))
for role, services in six.iteritems(static_ips): for role, services in six.iteritems(static_ips):
@ -514,7 +514,7 @@ def main():
try: try:
warnings = validate_node_pool_size(plan_env_path, ip_pools_path, warnings = validate_node_pool_size(plan_env_path, ip_pools_path,
template_files) template_files)
except Exception as e: except IOError as e:
errors.append("{}".format(e)) errors.append("{}".format(e))
if errors: if errors:

View File

@ -74,7 +74,7 @@ def open_network_environment_files(netenv_path, template_files):
try: try:
network_data = yaml_safe_load(template_files[netenv_path]) network_data = yaml_safe_load(template_files[netenv_path])
except Exception as e: except IOError as e:
return ({}, {}, ["Can't open network environment file '{}': {}" return ({}, {}, ["Can't open network environment file '{}': {}"
.format(netenv_path, e)]) .format(netenv_path, e)])
nic_configs = [] nic_configs = []
@ -87,7 +87,7 @@ def open_network_environment_files(netenv_path, template_files):
nic_configs.append(( nic_configs.append((
nic_name, nic_config_path, nic_name, nic_config_path,
yaml_safe_load(template_files[nic_config_path]))) yaml_safe_load(template_files[nic_config_path])))
except Exception as e: except IOError as e:
errors.append( errors.append(
"Can't open the resource '{}' reference file '{}': {}" "Can't open the resource '{}' reference file '{}': {}"
.format(nic_name, nic_config_path, e)) .format(nic_name, nic_config_path, e))