1 # -*- coding: utf-8 -*-
8 :copyright: (c) 2009 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 def __init__(self, name):
33 IOError.__init__(self, name)
37 class TemplateSyntaxError(TemplateError):
38 """Raised to tell the user that there is a problem with the template."""
40 def __init__(self, message, lineno, name=None, filename=None):
41 TemplateError.__init__(self, message)
44 self.filename = filename
47 # this is set to True if the debug.translate_syntax_error
48 # function translated the syntax error into a new traceback
49 self.translated = False
51 def __unicode__(self):
52 # for translated errors we only return the message
54 return self.message.encode('utf-8')
56 # otherwise attach some stuff
57 location = 'line %d' % self.lineno
58 name = self.filename or self.name
60 location = 'File "%s", %s' % (name, location)
61 lines = [self.message, ' ' + location]
63 # if the source is set, add the line to the output
64 if self.source is not None:
66 line = self.source.splitlines()[self.lineno - 1]
70 lines.append(' ' + line.strip())
72 return u'\n'.join(lines)
75 return unicode(self).encode('utf-8')
78 class TemplateAssertionError(TemplateSyntaxError):
79 """Like a template syntax error, but covers cases where something in the
80 template caused an error at compile time that wasn't necessarily caused
81 by a syntax error. However it's a direct subclass of
82 :exc:`TemplateSyntaxError` and has the same attributes.
86 class TemplateRuntimeError(TemplateError):
87 """A generic runtime error in the template engine. Under some situations
88 Jinja may raise this exception.
92 class UndefinedError(TemplateRuntimeError):
93 """Raised if a template tries to operate on :class:`Undefined`."""
96 class SecurityError(TemplateRuntimeError):
97 """Raised if a template tries to do something insecure if the
102 class FilterArgumentError(TemplateRuntimeError):
103 """This error is raised if a filter was called with inappropriate