Todo: Clarify error handling in `__getattr__` method
Ruchip16 opened this issue · 0 comments
Ruchip16 commented
The current implementation of the __getattr__
method lacks clarity in the error handling logic when the requested module is not found. The existing TODO comment raises a question about the appropriate approach for handling this scenario.
Code Snippet:
def __getattr__(self, name):
"""Run the ansible module matching the provided `name`.
Raise `AnsibleModuleError` when no such module exists.
"""
if not self.has_module(name):
# TODO: should we just raise an AttributeError, or a more
# suitable error handling strategy?
self.options["module_name"] = name
return self._run
Proposed Solution:
To enhance code readability and provide a consistent error handling strategy, let's decide between two possible solutions:
- Raise AttributeError Exception: Modify the code to raise a
AttributeError
exception when the requested module is not found. This aligns with the standard behavior in Python when accessing nonexistent attributes.
if not self.has_module(name):
raise AttributeError(f"Module {name} not found.")
- Keep AnsibleModuleError: Since the function states to raise
AnsibleModuleError
exception,AttributeError
exception gets eliminated
if not self.has_module(name):
raise AnsibleModuleError(
f"The module {name} was not found in configured module paths."
)