summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/templite.py12
-rw-r--r--tests/test_templite.py6
2 files changed, 14 insertions, 4 deletions
diff --git a/coverage/templite.py b/coverage/templite.py
index c2e8981f..c42e380c 100644
--- a/coverage/templite.py
+++ b/coverage/templite.py
@@ -11,6 +11,11 @@ class TempliteSyntaxError(ValueError):
pass
+class TempliteValueError(ValueError):
+ """Raised when an expression won't evaluate in a template."""
+ pass
+
+
class CodeBuilder(object):
"""Build source code conveniently."""
@@ -250,7 +255,12 @@ class Templite(object):
try:
value = getattr(value, dot)
except AttributeError:
- value = value[dot]
+ try:
+ value = value[dot]
+ except (TypeError, KeyError):
+ raise TempliteValueError(
+ "Couldn't evaluate %r.%s" % (value, dot)
+ )
if callable(value):
value = value()
return value
diff --git a/tests/test_templite.py b/tests/test_templite.py
index aca848a9..2b64e4e3 100644
--- a/tests/test_templite.py
+++ b/tests/test_templite.py
@@ -3,7 +3,7 @@
import re
-from coverage.templite import Templite, TempliteSyntaxError
+from coverage.templite import Templite, TempliteSyntaxError, TempliteValueError
from tests.coveragetest import CoverageTest
@@ -244,8 +244,8 @@ class TempliteTest(CoverageTest):
def test_exception_during_evaluation(self):
# TypeError: Couldn't evaluate {{ foo.bar.baz }}:
- # 'NoneType' object is unsubscriptable
- with self.assertRaises(TypeError):
+ msg = "Couldn't evaluate None.bar"
+ with self.assertRaisesRegex(TempliteValueError, msg):
self.try_render(
"Hey {{foo.bar.baz}} there", {'foo': None}, "Hey ??? there"
)