diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-01-09 20:10:23 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-01-09 20:10:23 -0500 |
commit | 0d1bc9fa8a3723c1929138f3b6feb1cfdb478e07 (patch) | |
tree | 3432209feff8054af73ef3a55ccaf7769d67fd20 | |
parent | eca68a4d21e6cc695417b2de21ec88ca0fa0921a (diff) | |
download | python-coveragepy-git-0d1bc9fa8a3723c1929138f3b6feb1cfdb478e07.tar.gz |
Report a more useful error if an expression fails.
-rw-r--r-- | coverage/templite.py | 12 | ||||
-rw-r--r-- | tests/test_templite.py | 6 |
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" ) |