Commit 85c52743 authored by Nick Johnson's avatar Nick Johnson Committed by Tim Graham
Browse files

Refs #18773 -- Improved template variable exception logging message.

parent 4b1416d3
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -902,8 +902,13 @@ class Variable(object):
                            else:
                                raise
        except Exception as e:
            template_name = getattr(context, 'template_name', 'unknown')
            logger.debug('{} - {}'.format(template_name, e))
            template_name = getattr(context, 'template_name', None) or 'unknown'
            logger.debug(
                "Exception while resolving variable '%s' in template '%s'.",
                bit,
                template_name,
                exc_info=True,
            )

            if getattr(e, 'silent_variable_failure', False):
                current = context.template.engine.string_if_invalid
+15 −6
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ class VariableResolveLoggingTests(SimpleTestCase):

            @property
            def template_name(self):
                return "template"
                return "template_name"

            @property
            def template(self):
@@ -51,19 +51,28 @@ class VariableResolveLoggingTests(SimpleTestCase):
                return self.__dict__[item]

        Variable('article').resolve(TestObject())

        self.assertEqual(
            self.test_handler.log_record.msg,
            'template - Attribute does not exist.'
            self.test_handler.log_record.getMessage(),
            "Exception while resolving variable 'article' in template 'template_name'."
        )
        self.assertIsNotNone(self.test_handler.log_record.exc_info)
        raised_exception = self.test_handler.log_record.exc_info[1]
        self.assertEqual(str(raised_exception), 'Attribute does not exist.')

    def test_log_on_variable_does_not_exist_not_silent(self):
        with self.assertRaises(VariableDoesNotExist):
            Variable('article.author').resolve({'article': {'section': 'News'}})

        self.assertEqual(
            self.test_handler.log_record.msg,
            'unknown - Failed lookup for key [author] in %r' %
            ("{%r: %r}" % ('section', 'News'), )
            self.test_handler.log_record.getMessage(),
            "Exception while resolving variable 'author' in template 'unknown'."
        )
        self.assertIsNotNone(self.test_handler.log_record.exc_info)
        raised_exception = self.test_handler.log_record.exc_info[1]
        self.assertEqual(
            str(raised_exception),
            'Failed lookup for key [author] in %r' % ("{%r: %r}" % ('section', 'News'))
        )

    def test_no_log_when_variable_exists(self):