From 101e983f07fbd4ffd3f5cd041bebc342f6fe5437 Mon Sep 17 00:00:00 2001 From: Thomas Quinot Date: Fri, 26 Jan 2018 18:07:58 +0100 Subject: [PATCH] Remove incorrect assumption on exception constructors (#35271) Do not assume that all exception constructors accept a single string argument. For example UnicodeError's __init__ takes 5 parameters: encoding, object, start, end, reason. Also, if e.message is present but empty, fall back on stringifying the exception occurrence. Fixes #35270 --- lib/ansible/template/vars.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/ansible/template/vars.py b/lib/ansible/template/vars.py index 7ce650ff7d..1b95a3eba4 100644 --- a/lib/ansible/template/vars.py +++ b/lib/ansible/template/vars.py @@ -23,6 +23,7 @@ from collections import Mapping from jinja2.utils import missing +from ansible.errors import AnsibleError from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -105,10 +106,10 @@ class AnsibleJ2Vars(Mapping): try: value = self._templar.template(variable) except Exception as e: - try: - raise type(e)(to_native(variable) + ': ' + e.message) - except AttributeError: - raise type(e)(to_native(variable) + ': ' + to_native(e)) + msg = getattr(e, 'message') or to_native(e) + raise AnsibleError("An unhandled exception occurred while templating '%s'. " + "Error was a %s, original message: %s" % (to_native(variable), type(e), msg)) + return value def add_locals(self, locals):