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

This commit is contained in:
Nick Johnson 2015-09-21 12:20:19 -07:00 committed by Tim Graham
parent 4b1416d372
commit 85c52743f7
2 changed files with 22 additions and 8 deletions

View File

@ -902,8 +902,13 @@ class Variable(object):
else: else:
raise raise
except Exception as e: except Exception as e:
template_name = getattr(context, 'template_name', 'unknown') template_name = getattr(context, 'template_name', None) or 'unknown'
logger.debug('{} - {}'.format(template_name, e)) logger.debug(
"Exception while resolving variable '%s' in template '%s'.",
bit,
template_name,
exc_info=True,
)
if getattr(e, 'silent_variable_failure', False): if getattr(e, 'silent_variable_failure', False):
current = context.template.engine.string_if_invalid current = context.template.engine.string_if_invalid

View File

@ -34,7 +34,7 @@ class VariableResolveLoggingTests(SimpleTestCase):
@property @property
def template_name(self): def template_name(self):
return "template" return "template_name"
@property @property
def template(self): def template(self):
@ -51,19 +51,28 @@ class VariableResolveLoggingTests(SimpleTestCase):
return self.__dict__[item] return self.__dict__[item]
Variable('article').resolve(TestObject()) Variable('article').resolve(TestObject())
self.assertEqual( self.assertEqual(
self.test_handler.log_record.msg, self.test_handler.log_record.getMessage(),
'template - Attribute does not exist.' "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): def test_log_on_variable_does_not_exist_not_silent(self):
with self.assertRaises(VariableDoesNotExist): with self.assertRaises(VariableDoesNotExist):
Variable('article.author').resolve({'article': {'section': 'News'}}) Variable('article.author').resolve({'article': {'section': 'News'}})
self.assertEqual( self.assertEqual(
self.test_handler.log_record.msg, self.test_handler.log_record.getMessage(),
'unknown - Failed lookup for key [author] in %r' % "Exception while resolving variable 'author' in template 'unknown'."
("{%r: %r}" % ('section', 'News'), ) )
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): def test_no_log_when_variable_exists(self):