From b87edda3c7b3587f3bd786059beb5bbbf2450526 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Mon, 6 Jun 2022 20:30:55 +1200 Subject: [PATCH] ModuleHelperException module utils - improved exception initialization (#4755) * ModuleHelperException module utils - improved exception initialization * added changelog fragment * Update plugins/module_utils/mh/exceptions.py Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- .../fragments/4755-mhexception-improvement.yml | 2 ++ plugins/module_utils/mh/exceptions.py | 18 +++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/4755-mhexception-improvement.yml 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)