1 # -*- coding: utf-8 -*-
8 :copyright: (c) 2010 by the Jinja Team.
9 :license: BSD, see LICENSE for more details.
13 class TemplateError(Exception):
14 """Baseclass for all template errors."""
16 def __init__(self
, message
=None):
17 if message
is not None:
18 message
= unicode(message
).encode('utf-8')
19 Exception.__init
__(self
, message
)
24 message
= self
.args
[0]
25 if message
is not None:
26 return message
.decode('utf-8', 'replace')
29 class TemplateNotFound(IOError, LookupError, TemplateError
):
30 """Raised if a template does not exist."""
32 # looks weird, but removes the warning descriptor that just
33 # bogusly warns us about message being deprecated
36 def __init__(self
, name
, message
=None):
37 IOError.__init
__(self
)
40 self
.message
= message
42 self
.templates
= [name
]
45 return self
.message
.encode('utf-8')
47 # unicode goes after __str__ because we configured 2to3 to rename
48 # __unicode__ to __str__. because the 2to3 tree is not designed to
49 # remove nodes from it, we leave the above __str__ around and let
50 # it override at runtime.
51 def __unicode__(self
):
55 class TemplatesNotFound(TemplateNotFound
):
56 """Like :class:`TemplateNotFound` but raised if multiple templates
57 are selected. This is a subclass of :class:`TemplateNotFound`
58 exception, so just catching the base exception will catch both.
63 def __init__(self
, names
=(), message
=None):
65 message
= u
'non of the templates given were found: ' + \
66 u
', '.join(map(unicode, names
))
67 TemplateNotFound
.__init
__(self
, names
and names
[-1] or None, message
)
68 self
.templates
= list(names
)
71 class TemplateSyntaxError(TemplateError
):
72 """Raised to tell the user that there is a problem with the template."""
74 def __init__(self
, message
, lineno
, name
=None, filename
=None):
75 TemplateError
.__init
__(self
, message
)
78 self
.filename
= filename
81 # this is set to True if the debug.translate_syntax_error
82 # function translated the syntax error into a new traceback
83 self
.translated
= False
86 return unicode(self
).encode('utf-8')
88 # unicode goes after __str__ because we configured 2to3 to rename
89 # __unicode__ to __str__. because the 2to3 tree is not designed to
90 # remove nodes from it, we leave the above __str__ around and let
91 # it override at runtime.
92 def __unicode__(self
):
93 # for translated errors we only return the message
97 # otherwise attach some stuff
98 location
= 'line %d' % self
.lineno
99 name
= self
.filename
or self
.name
101 location
= 'File "%s", %s' % (name
, location
)
102 lines
= [self
.message
, ' ' + location
]
104 # if the source is set, add the line to the output
105 if self
.source
is not None:
107 line
= self
.source
.splitlines()[self
.lineno
- 1]
111 lines
.append(' ' + line
.strip())
113 return u
'\n'.join(lines
)
116 class TemplateAssertionError(TemplateSyntaxError
):
117 """Like a template syntax error, but covers cases where something in the
118 template caused an error at compile time that wasn't necessarily caused
119 by a syntax error. However it's a direct subclass of
120 :exc:`TemplateSyntaxError` and has the same attributes.
124 class TemplateRuntimeError(TemplateError
):
125 """A generic runtime error in the template engine. Under some situations
126 Jinja may raise this exception.
130 class UndefinedError(TemplateRuntimeError
):
131 """Raised if a template tries to operate on :class:`Undefined`."""
134 class SecurityError(TemplateRuntimeError
):
135 """Raised if a template tries to do something insecure if the
140 class FilterArgumentError(TemplateRuntimeError
):
141 """This error is raised if a filter was called with inappropriate