Skip to content

Commit

Permalink
mh guide: update exception handling (#9282)
Browse files Browse the repository at this point in the history
(cherry picked from commit dcdec6e)
  • Loading branch information
russoz authored and patchback[bot] committed Dec 20, 2024
1 parent 6e42e44 commit d6a1138
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/docsite/rst/guide_modulehelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ However, you can set output variables specifically for that exception, if you so

.. code-block:: python
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelperException
def __init_module__(self):
if not complex_validation():
self.do_raise("Validation failed!")
Expand All @@ -354,11 +356,16 @@ However, you can set output variables specifically for that exception, if you so
awesomeness = calculate_awesomeness()
if awesomeness > 1000:
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.
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.

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:

StateModuleHelper
Expand Down

0 comments on commit d6a1138

Please sign in to comment.