blob: 69a877ba3b0be87d08bbb44c72efd189334b22f1 [file] [log] [blame]
Armin Ronacher07bc6842008-03-31 14:18:49 +02001# -*- coding: utf-8 -*-
2"""
Armin Ronacher26c0f512008-04-17 11:16:01 +02003 jinja2.exceptions
4 ~~~~~~~~~~~~~~~~~
Armin Ronacher07bc6842008-03-31 14:18:49 +02005
6 Jinja exceptions.
7
Armin Ronacher55494e42010-01-22 09:41:48 +01008 :copyright: (c) 2010 by the Jinja Team.
Armin Ronacher07bc6842008-03-31 14:18:49 +02009 :license: BSD, see LICENSE for more details.
10"""
11
12
Armin Ronachere791c2a2008-04-07 18:39:54 +020013class TemplateError(Exception):
Armin Ronacher9a822052008-04-17 18:44:07 +020014 """Baseclass for all template errors."""
15
Armin Ronacher1d021082009-02-19 20:07:13 +010016 def __init__(self, message=None):
17 if message is not None:
18 message = unicode(message).encode('utf-8')
19 Exception.__init__(self, message)
20
21 @property
22 def message(self):
23 if self.args:
24 message = self.args[0]
25 if message is not None:
26 return message.decode('utf-8', 'replace')
27
Armin Ronacher9a822052008-04-17 18:44:07 +020028
Armin Ronacher07bc6842008-03-31 14:18:49 +020029class TemplateNotFound(IOError, LookupError, TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020030 """Raised if a template does not exist."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020031
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010032 # looks weird, but removes the warning descriptor that just
33 # bogusly warns us about message being deprecated
34 message = None
35
36 def __init__(self, name, message=None):
37 IOError.__init__(self)
38 if message is None:
39 message = name
40 self.message = message
Armin Ronacher07bc6842008-03-31 14:18:49 +020041 self.name = name
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010042 self.templates = [name]
43
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010044 def __str__(self):
45 return self.message.encode('utf-8')
46
Armin Ronacheracbd4082010-02-10 00:07:43 +010047 # 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.
Armin Ronacher790b8a82010-02-10 00:05:46 +010051 def __unicode__(self):
52 return self.message
53
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010054
55class 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.
59
60 .. versionadded:: 2.2
61 """
62
63 def __init__(self, names=(), message=None):
64 if message is None:
65 message = u'non of the templates given were found: ' + \
Armin Ronacher7e40df02010-01-14 00:54:47 +010066 u', '.join(map(unicode, names))
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010067 TemplateNotFound.__init__(self, names and names[-1] or None, message)
68 self.templates = list(names)
Armin Ronacher07bc6842008-03-31 14:18:49 +020069
70
Armin Ronacher68f77672008-04-17 11:50:39 +020071class TemplateSyntaxError(TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020072 """Raised to tell the user that there is a problem with the template."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020073
Armin Ronacher7f15ef82008-05-16 09:11:39 +020074 def __init__(self, message, lineno, name=None, filename=None):
Armin Ronacher1d021082009-02-19 20:07:13 +010075 TemplateError.__init__(self, message)
Armin Ronacher07bc6842008-03-31 14:18:49 +020076 self.lineno = lineno
Armin Ronacher7f15ef82008-05-16 09:11:39 +020077 self.name = name
Armin Ronacheraaf010d2008-05-01 13:14:30 +020078 self.filename = filename
Armin Ronacherccae0552008-10-05 23:08:58 +020079 self.source = None
Armin Ronacherccae0552008-10-05 23:08:58 +020080
Armin Ronacherd416a972009-02-24 22:58:00 +010081 # 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
84
Armin Ronacher790b8a82010-02-10 00:05:46 +010085 def __str__(self):
86 return unicode(self).encode('utf-8')
87
Armin Ronacheracbd4082010-02-10 00:07:43 +010088 # 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.
Armin Ronacherccae0552008-10-05 23:08:58 +020092 def __unicode__(self):
Armin Ronacherd416a972009-02-24 22:58:00 +010093 # for translated errors we only return the message
94 if self.translated:
95 return self.message.encode('utf-8')
96
97 # otherwise attach some stuff
Armin Ronacherccae0552008-10-05 23:08:58 +020098 location = 'line %d' % self.lineno
99 name = self.filename or self.name
100 if name:
101 location = 'File "%s", %s' % (name, location)
102 lines = [self.message, ' ' + location]
103
104 # if the source is set, add the line to the output
105 if self.source is not None:
106 try:
107 line = self.source.splitlines()[self.lineno - 1]
108 except IndexError:
109 line = None
110 if line:
111 lines.append(' ' + line.strip())
112
113 return u'\n'.join(lines)
114
Armin Ronacher07bc6842008-03-31 14:18:49 +0200115
Armin Ronacher53042292008-04-26 18:30:19 +0200116class TemplateAssertionError(TemplateSyntaxError):
Armin Ronacher9a822052008-04-17 18:44:07 +0200117 """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
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200119 by a syntax error. However it's a direct subclass of
120 :exc:`TemplateSyntaxError` and has the same attributes.
Armin Ronacher9a822052008-04-17 18:44:07 +0200121 """
Armin Ronachere791c2a2008-04-07 18:39:54 +0200122
Armin Ronachere791c2a2008-04-07 18:39:54 +0200123
Armin Ronacher07bc6842008-03-31 14:18:49 +0200124class TemplateRuntimeError(TemplateError):
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200125 """A generic runtime error in the template engine. Under some situations
126 Jinja may raise this exception.
127 """
Benjamin Wieganda3152742008-04-28 18:07:52 +0200128
129
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200130class UndefinedError(TemplateRuntimeError):
131 """Raised if a template tries to operate on :class:`Undefined`."""
132
133
134class SecurityError(TemplateRuntimeError):
135 """Raised if a template tries to do something insecure if the
136 sandbox is enabled.
137 """
138
139
140class FilterArgumentError(TemplateRuntimeError):
Benjamin Wieganda3152742008-04-28 18:07:52 +0200141 """This error is raised if a filter was called with inappropriate
142 arguments
143 """