From 64994c781b7ce687af8650306e7656036cb2feba Mon Sep 17 00:00:00 2001 From: Jiri Podivin Date: Thu, 15 Apr 2021 13:32:16 +0200 Subject: [PATCH] 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 Change-Id: I48b237d1a8f3bab3b0817db5edb9aade0a941e42 --- library/network_environment.py | 14 +++++++------- library/switch_vlans.py | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/library/network_environment.py b/library/network_environment.py index 9fd3c3adc..64f34cebb 100644 --- a/library/network_environment.py +++ b/library/network_environment.py @@ -76,7 +76,7 @@ def open_network_environment_files(netenv_path, template_files): try: network_data = yaml_safe_load(template_files[netenv_path]) - except Exception as e: + except IOError as e: return ({}, {}, ["Can't open network environment file '{}': {}" .format(netenv_path, e)]) nic_configs = [] @@ -89,7 +89,7 @@ def open_network_environment_files(netenv_path, template_files): nic_configs.append(( nic_name, nic_config_path, yaml_safe_load(template_files[nic_config_path]))) - except Exception as e: + except IOError as e: errors.append( "Can't open the resource '{}' reference file '{}': {}" .format(nic_name, nic_config_path, e)) @@ -254,7 +254,7 @@ def check_allocation_pools_pairing(filedata, pools): pool_objs.append(netaddr.IPRange( netaddr.IPAddress(dict_range['start']), netaddr.IPAddress(dict_range['end']))) - except Exception: + except (ValueError, TypeError, KeyError, netaddr.AddrFormatError): errors.append("Invalid format of the IP range in {}: {}" .format(poolitem, dict_range)) continue @@ -268,7 +268,7 @@ def check_allocation_pools_pairing(filedata, pools): errors.append('The {} CIDR is not specified for {}.' .format(subnet_item, poolitem)) continue - except Exception: + except (netaddr.AddrFormatError, ValueError): errors.append('Invalid IP network: {}'.format(network)) continue @@ -323,7 +323,7 @@ def check_static_ip_pool_collision(static_ips, pools): try: ip_range = netaddr.IPRange(allocation_range['start'], allocation_range['end']) - except Exception: + except (netaddr.AddrFormatError, TypeError, KeyError): errors.append("Invalid format of the IP range in {}: {}" .format(pool_name, allocation_range)) continue @@ -398,7 +398,7 @@ def check_static_ip_in_cidr(networks, static_ips): for name, cidr in six.iteritems(networks): try: network_ranges[name] = netaddr.IPNetwork(cidr) - except Exception: + except (netaddr.AddrFormatError, ValueError): errors.append("Network '{}' has an invalid CIDR: '{}'" .format(name, cidr)) for role, services in six.iteritems(static_ips): @@ -514,7 +514,7 @@ def main(): try: warnings = validate_node_pool_size(plan_env_path, ip_pools_path, template_files) - except Exception as e: + except IOError as e: errors.append("{}".format(e)) if errors: diff --git a/library/switch_vlans.py b/library/switch_vlans.py index 653f16df8..06973bbc7 100644 --- a/library/switch_vlans.py +++ b/library/switch_vlans.py @@ -74,7 +74,7 @@ def open_network_environment_files(netenv_path, template_files): try: network_data = yaml_safe_load(template_files[netenv_path]) - except Exception as e: + except IOError as e: return ({}, {}, ["Can't open network environment file '{}': {}" .format(netenv_path, e)]) nic_configs = [] @@ -87,7 +87,7 @@ def open_network_environment_files(netenv_path, template_files): nic_configs.append(( nic_name, nic_config_path, yaml_safe_load(template_files[nic_config_path]))) - except Exception as e: + except IOError as e: errors.append( "Can't open the resource '{}' reference file '{}': {}" .format(nic_name, nic_config_path, e))