Skip to content
Myr

Improved error message for undefineds

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 7d29ec6 · Browse files · 3 files+34 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+34 added−9 removed

  • other:+34 added−9 removed

3 files+34 added−9 removed

modified:jinja2/testsuite/api.py+11 added-3 removed
205 unmodified lines
206 def test_indexing_gives_undefined(self):207 t = Template("{{ var[42].foo }}")208 assert_raises(UndefinedError, t.render, var=0)209210 def test_none_gives_propert_error(self):210 def test_none_gives_proper_error(self):211 try:212 Undefined(None).split()212 Environment().getattr(None, 'split')213 except UndefinedError, e:214 assert e.message == 'None is not defined'214 assert e.message == "None has no attribute 'split'"215 else:216 assert False, 'expected exception'217218 def test_object_repr(self):219 try:220 Undefined(obj=42, name='upper')221 except UndefinedError, e:222 assert e.message == "'int' object has no attribute 'upper'"223 else:224 assert False, 'expected exception'225226227def suite():228 suite = unittest.TestSuite()229 suite.addTest(unittest.makeSuite(ExtendedAPITestCase))230 suite.addTest(unittest.makeSuite(MetaTestCase))
2 unmodified lines
modified:jinja2/runtime.py+7 added-6 removed
10 unmodified lines
11import sys12from itertools import chain, imap13from jinja2.nodes import EvalContext14from jinja2.utils import Markup, partial, soft_unicode, escape, missing, \15 concat, MethodType, FunctionType, internalcode, next15 concat, MethodType, FunctionType, internalcode, next, \16 object_type_repr17from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \18 TemplateNotFound1920
416 unmodified lines
437 if self._undefined_hint is None:438 if self._undefined_obj is missing:439 hint = '%r is undefined' % self._undefined_name440 elif not isinstance(self._undefined_name, basestring):440 hint = '%r object has no element %r' % (441 self._undefined_obj.__class__.__name__,441 hint = '%s has no element %r' % (442 object_type_repr(self._undefined_obj),443 self._undefined_name444 )445 else:445 hint = '%r object has no attribute %r' % (446 self._undefined_obj.__class__.__name__,446 hint = '%r has no attribute %r' % (447 object_type_repr(self._undefined_obj),448 self._undefined_name449 )450 else:451 hint = self._undefined_hint
49 unmodified lines
501 if self._undefined_hint is None:502 if self._undefined_obj is missing:503 return u'{{ %s }}' % self._undefined_name504 return '{{ no such element: %s[%r] }}' % (504 self._undefined_obj.__class__.__name__,505 object_type_repr(self._undefined_obj),506 self._undefined_name507 )508 return u'{{ undefined value printed: %s }}' % self._undefined_hint509
28 unmodified lines
modified:jinja2/utils.py+16 added
225 unmodified lines
226 if e.errno not in (errno.ENOENT, errno.EISDIR):227 raise228229230def object_type_repr(obj):231 """Returns the name of the object's type. For some recognized232 singletons the name of the object is returned instead. (For233 example for `None` and `Ellipsis`).234 """235 if obj is None:236 return 'None'237 elif obj is Ellipsis:238 return 'Ellipsis'239 if obj.__class__.__module__ == '__builtin__':240 name = obj.__name__241 else:242 name = obj.__class__.module__ + '.' + obj.__name__243 return '%s object' % name244245246def pformat(obj, verbose=False):247 """Prettyprint an object. Either use the `pretty` library or the248 builtin `pprint`.249 """
569 unmodified lines