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 | 35fda609e4dc07d0b6d47deb8746eec3ac3729b4 (patch) | |
| tree | cdf93373577dfeb584df4bde479e660abf7bfb34 | |
| parent | 70348a1a6e422cf271e432319b24fb66d30d1559 (diff) | |
| download | python-coveragepy-35fda609e4dc07d0b6d47deb8746eec3ac3729b4.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 c2e8981..c42e380 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 aca848a..2b64e4e 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" ) |
