From b3251c9585b0b0180fcdf09748e9a0dc439bc1aa Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 27 Feb 2017 13:29:40 -0500 Subject: [PATCH] Exceptit (#21864) * more centralized exception handling * only remove when verboxse --- lib/ansible/plugins/callback/__init__.py | 15 +++++++++++++++ lib/ansible/plugins/callback/default.py | 22 +++------------------- lib/ansible/plugins/callback/minimal.py | 10 +--------- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/lib/ansible/plugins/callback/__init__.py b/lib/ansible/plugins/callback/__init__.py index 162ff1d0a24..2ac1ef22249 100644 --- a/lib/ansible/plugins/callback/__init__.py +++ b/lib/ansible/plugins/callback/__init__.py @@ -109,6 +109,20 @@ class CallbackBase: self._display.deprecated(**warning) del res['deprecations'] + def _handle_exception(self, result): + + if 'exception' in result: + msg = "An exception occurred during task execution. " + if self._display.verbosity < 3: + # extract just the actual error message from the exception text + error = result['exception'].strip().split('\n')[-1] + msg += "To see the full traceback, use -vvv. The error was: %s" % error + else: + msg = "The full traceback is:\n" + result['exception'] + del result['exception'] + + self._display.display(msg, color=C.COLOR_ERROR) + def _get_diff(self, difflist): if not isinstance(difflist, list): @@ -197,6 +211,7 @@ class CallbackBase: if 'invocation' in result and task_name in ['debug']: del result['invocation'] + def set_play_context(self, play_context): pass diff --git a/lib/ansible/plugins/callback/default.py b/lib/ansible/plugins/callback/default.py index d924b175a5d..779d3f4f153 100644 --- a/lib/ansible/plugins/callback/default.py +++ b/lib/ansible/plugins/callback/default.py @@ -46,16 +46,7 @@ class CallbackModule(CallbackBase): self._print_task_banner(result._task) delegated_vars = result._result.get('_ansible_delegated_vars', None) - if 'exception' in result._result: - if self._display.verbosity < 3: - # extract just the actual error message from the exception text - error = result._result['exception'].strip().split('\n')[-1] - msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error - else: - msg = "An exception occurred during task execution. The full traceback is:\n" + result._result['exception'] - - self._display.display(msg, color=C.COLOR_ERROR) - + self._handle_exception(result._result) self._handle_warnings(result._result) if result._task.loop and 'results' in result._result: @@ -212,16 +203,9 @@ class CallbackModule(CallbackBase): self._display.display(msg, color=color) def v2_runner_item_on_failed(self, result): - delegated_vars = result._result.get('_ansible_delegated_vars', None) - if 'exception' in result._result: - if self._display.verbosity < 3: - # extract just the actual error message from the exception text - error = result._result['exception'].strip().split('\n')[-1] - msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error - else: - msg = "An exception occurred during task execution. The full traceback is:\n" + result._result['exception'] - self._display.display(msg, color=C.COLOR_ERROR) + delegated_vars = result._result.get('_ansible_delegated_vars', None) + self._handle_exception(result._result) msg = "failed: " if delegated_vars: diff --git a/lib/ansible/plugins/callback/minimal.py b/lib/ansible/plugins/callback/minimal.py index 9df64d3cc0c..de10b3d17ae 100644 --- a/lib/ansible/plugins/callback/minimal.py +++ b/lib/ansible/plugins/callback/minimal.py @@ -45,16 +45,8 @@ class CallbackModule(CallbackBase): return buf + "\n" def v2_runner_on_failed(self, result, ignore_errors=False): - if 'exception' in result._result: - if self._display.verbosity < 3: - # extract just the actual error message from the exception text - error = result._result['exception'].strip().split('\n')[-1] - msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error - else: - msg = "An exception occurred during task execution. The full traceback is:\n" + result._result['exception'] - - self._display.display(msg, color=C.COLOR_ERROR) + self._handle_exception(result._result) self._handle_warnings(result._result) if result._task.action in C.MODULE_NO_JSON and 'module_stderr' not in result._result: