Open
Description
Background
In PR #323, CodeRabbit suggested refactoring exception message handling by introducing custom exception classes to address Ruff TRY003 warnings about long messages outside exception classes.
Current Approach
Currently, the codebase uses direct exception raising with detailed error messages that include CWE references, such as:
except ImportError as baton:
raise ImportError("[CWE-440] Unable to import sys module.") from baton
Proposed Solution
Implement custom exception classes to encapsulate these messages for better maintainability:
class ImportFailureError(ImportError):
"""Custom exception for import failures with CWE references."""
def __init__(self, component, cwe="440", *args, **kwargs):
self.cwe = cwe
message = f"[CWE-{cwe}] Unable to import {component}."
super().__init__(message, *args, **kwargs)
# Usage example:
except ImportError as baton:
raise ImportFailureError("sys module", "440") from baton
Benefits
- Improved code maintainability
- Consistent error messaging
- Addresses Ruff TRY003 linting warnings
- Better encapsulation of error information