diff --git a/changelogs/fragments/4755-mhexception-improvement.yml b/changelogs/fragments/4755-mhexception-improvement.yml new file mode 100644 index 0000000000..ebadb98bc5 --- /dev/null +++ b/changelogs/fragments/4755-mhexception-improvement.yml @@ -0,0 +1,2 @@ +minor_changes: + - ModuleHelper module utils - improved ``ModuleHelperException``, using ``to_native()`` for the exception message (https://github.com/ansible-collections/community.general/pull/4755). diff --git a/plugins/module_utils/mh/exceptions.py b/plugins/module_utils/mh/exceptions.py index 7d1a46bb5a..2ee548ff41 100644 --- a/plugins/module_utils/mh/exceptions.py +++ b/plugins/module_utils/mh/exceptions.py @@ -6,17 +6,13 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +from ansible.module_utils.common.text.converters import to_native + class ModuleHelperException(Exception): - @staticmethod - def _get_remove(key, kwargs): - if key in kwargs: - result = kwargs[key] - del kwargs[key] - return result - return None - - def __init__(self, *args, **kwargs): - self.msg = self._get_remove('msg', kwargs) or "Module failed with exception: {0}".format(self) - self.update_output = self._get_remove('update_output', kwargs) or {} + def __init__(self, msg, update_output=None, *args, **kwargs): + self.msg = to_native(msg or "Module failed with exception: {0}".format(self)) + if update_output is None: + update_output = {} + self.update_output = update_output super(ModuleHelperException, self).__init__(*args)