python: replace bare except clauses with specific exception types#5074
python: replace bare except clauses with specific exception types#5074
Conversation
Bare except clauses catch all exceptions including KeyboardInterrupt and SystemExit, which can mask critical errors and prevent proper program termination. Replace each bare except with the appropriate specific exception type: - interfaces_ethernet.py: except Exception (interface MTU lookup) - vpp_interfaces_bonding.py: except ValueError (MAC validation) - container.py: except IndexError (prefix list indexing) - config.py: except Exception (ACME certificate loading) - configverify.py: except Exception (MTU min/max lookup) - configverify.py: except (TypeError, ValueError) (DH key conversion) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
❌ |
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
There was a problem hiding this comment.
Pull request overview
This PR aims to improve Python exception handling safety by replacing bare except: clauses with specific exception types so that KeyboardInterrupt/SystemExit aren’t accidentally swallowed during config processing.
Changes:
- Replaced bare
except:withexcept ValueErroraround MAC validation in VPP bonding verification. - Replaced bare
except:withexcept IndexErrorin container IP prefix selection logic. - Replaced bare
except:withexcept Exception(and oneexcept (TypeError, ValueError)) in MTU/DH/acme-related helper functions.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/conf_mode/vpp_interfaces_bonding.py |
Narrows MAC validation exception handling to ValueError. |
src/conf_mode/interfaces_ethernet.py |
Narrows MTU clamping exception handling to Exception. |
src/conf_mode/container.py |
Narrows list-indexing exception handling to IndexError for prefix lookups. |
python/vyos/configverify.py |
Narrows MTU fallback exception handling to Exception; narrows DH keysize conversion handling to (TypeError, ValueError). |
python/vyos/config.py |
Narrows ACME cert/key loading exception handling to Exception. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- vpp_interfaces_bonding: forward actual ValueError message instead of hardcoded multicast error, since assert_mac raises for multiple reasons - container: catch KeyError and TypeError alongside IndexError for prefix lookups, since 'prefix' key may not exist Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try: | ||
| keysize = str(min_keysize) | ||
| except: | ||
| except (TypeError, ValueError): | ||
| return False |
There was a problem hiding this comment.
verify_diffie_hellman_length() currently wraps only str(min_keysize) in try/except, but the later int(min_keysize) conversion can still raise ValueError for inputs like 'abc', causing an exception instead of returning False. Also, the local variable keysize is unused. Consider validating/converting min_keysize once (e.g., to an int) inside the try/except and then using that value for comparisons.
|
CI integration 👍 passed! Details
|
Summary
except:clauses with specific exception types across 5 filesexcept:catches all exceptions includingKeyboardInterruptandSystemExit, which can mask critical errors and prevent proper program terminationChanges
src/conf_mode/interfaces_ethernet.py:162Exceptionsrc/conf_mode/vpp_interfaces_bonding.py:181ValueErrorassert_mac()raisesValueErroron invalid MACsrc/conf_mode/container.py:180IndexError[0]on empty filtered listsrc/conf_mode/container.py:186IndexError[0]on empty filtered listpython/vyos/config.py:124Exceptionpython/vyos/configverify.py:49Exceptionpython/vyos/configverify.py:436(TypeError, ValueError)str()conversion of DH keysize parameterTest plan
🤖 Generated with Claude Code