refactor: Extract large functions, improve exception handling, remove duplicates
## Large function extraction - servers.py: Extract 8 _impl functions from register_server_tools (449 lines) - certificates.py: Extract 7 _impl functions from register_certificate_tools (386 lines) - MCP tool wrappers now delegate to module-level implementation functions ## Exception handling improvements - Replace 11 broad `except Exception` with specific types - health.py: (OSError, subprocess.SubprocessError) - configuration.py: (HaproxyError, IOError, OSError, ValueError) - servers.py: (IOError, OSError, ValueError) - certificates.py: FileNotFoundError, (subprocess.SubprocessError, OSError) ## Duplicate code extraction - Add parse_servers_state() to utils.py (replaces 4 duplicate parsers) - Add disable_server_slot() to utils.py (replaces duplicate patterns) - Update health.py, servers.py, domains.py to use new helpers ## Other improvements - Add TypedDict types in file_ops.py and health.py - Set file permissions (0o600) for sensitive files - Update tests to use specific exception types Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,18 @@ def validate_port(port: str) -> bool:
|
||||
return 1 <= port_num <= 65535
|
||||
|
||||
|
||||
def validate_port_int(port: int) -> bool:
|
||||
"""Validate port number as integer is in valid range.
|
||||
|
||||
Args:
|
||||
port: Port number as integer
|
||||
|
||||
Returns:
|
||||
True if port is valid (1-65535), False otherwise
|
||||
"""
|
||||
return isinstance(port, int) and 1 <= port <= 65535
|
||||
|
||||
|
||||
def validate_backend_name(name: str) -> bool:
|
||||
"""Validate backend or server name to prevent command injection.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user