mh guide: update exception handling (#9282)

pull/9295/head
Alexei Znamensky 2024-12-19 20:42:45 +13:00 committed by GitHub
parent 50b25f8c01
commit dcdec6ee4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 0 deletions

View File

@ -346,6 +346,8 @@ However, you can set output variables specifically for that exception, if you so
.. code-block:: python .. code-block:: python
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelperException
def __init_module__(self): def __init_module__(self):
if not complex_validation(): if not complex_validation():
self.do_raise("Validation failed!") self.do_raise("Validation failed!")
@ -354,11 +356,16 @@ However, you can set output variables specifically for that exception, if you so
awesomeness = calculate_awesomeness() awesomeness = calculate_awesomeness()
if awesomeness > 1000: if awesomeness > 1000:
self.do_raise("Over awesome, I cannot handle it!", update_output={"awesomeness": awesomeness}) self.do_raise("Over awesome, I cannot handle it!", update_output={"awesomeness": awesomeness})
# which is just a convenience shortcut for
raise ModuleHelperException("...", update_output={...})
All exceptions derived from ``Exception`` are captured and translated into a ``fail_json()`` call. All exceptions derived from ``Exception`` are captured and translated into a ``fail_json()`` call.
However, if you do want to call ``self.module.fail_json()`` yourself it will work, However, if you do want to call ``self.module.fail_json()`` yourself it will work,
just keep in mind that there will be no automatic handling of output variables in that case. just keep in mind that there will be no automatic handling of output variables in that case.
Behind the curtains, all ``do_raise()`` does is to raise a ``ModuleHelperException``.
If you want to create specialized error handling for your code, the best way is to extend that clas and raise it when needed.
.. _ansible_collections.community.general.docsite.guide_modulehelper.statemh: .. _ansible_collections.community.general.docsite.guide_modulehelper.statemh:
StateModuleHelper StateModuleHelper