blob: 90025210ace5311b22da3729b767ed99cc559ff9 [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"""
Thomas Waldmanne0003552013-05-17 23:52:14 +020011import six
12from six.moves import map
13from six.moves import zip
Armin Ronacher07bc6842008-03-31 14:18:49 +020014
15
Armin Ronachere791c2a2008-04-07 18:39:54 +020016class TemplateError(Exception):
Armin Ronacher9a822052008-04-17 18:44:07 +020017 """Baseclass for all template errors."""
18
Armin Ronacher1d021082009-02-19 20:07:13 +010019 def __init__(self, message=None):
20 if message is not None:
Thomas Waldmanne0003552013-05-17 23:52:14 +020021 message = six.text_type(message).encode('utf-8')
Armin Ronacher1d021082009-02-19 20:07:13 +010022 Exception.__init__(self, message)
23
24 @property
25 def message(self):
26 if self.args:
27 message = self.args[0]
28 if message is not None:
29 return message.decode('utf-8', 'replace')
30
Armin Ronacher9a822052008-04-17 18:44:07 +020031
Armin Ronacher07bc6842008-03-31 14:18:49 +020032class TemplateNotFound(IOError, LookupError, TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020033 """Raised if a template does not exist."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020034
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010035 # looks weird, but removes the warning descriptor that just
36 # bogusly warns us about message being deprecated
37 message = None
38
39 def __init__(self, name, message=None):
40 IOError.__init__(self)
41 if message is None:
42 message = name
43 self.message = message
Armin Ronacher07bc6842008-03-31 14:18:49 +020044 self.name = name
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010045 self.templates = [name]
46
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010047 def __str__(self):
48 return self.message.encode('utf-8')
49
Armin Ronacheracbd4082010-02-10 00:07:43 +010050 # unicode goes after __str__ because we configured 2to3 to rename
51 # __unicode__ to __str__. because the 2to3 tree is not designed to
52 # remove nodes from it, we leave the above __str__ around and let
53 # it override at runtime.
Armin Ronacher790b8a82010-02-10 00:05:46 +010054 def __unicode__(self):
55 return self.message
56
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010057
58class TemplatesNotFound(TemplateNotFound):
59 """Like :class:`TemplateNotFound` but raised if multiple templates
60 are selected. This is a subclass of :class:`TemplateNotFound`
61 exception, so just catching the base exception will catch both.
62
63 .. versionadded:: 2.2
64 """
65
66 def __init__(self, names=(), message=None):
67 if message is None:
Simon Sapin52c88fb2011-12-03 13:05:08 +010068 message = u'none of the templates given were found: ' + \
Armin Ronacher7e40df02010-01-14 00:54:47 +010069 u', '.join(map(unicode, names))
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010070 TemplateNotFound.__init__(self, names and names[-1] or None, message)
71 self.templates = list(names)
Armin Ronacher07bc6842008-03-31 14:18:49 +020072
73
Armin Ronacher68f77672008-04-17 11:50:39 +020074class TemplateSyntaxError(TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020075 """Raised to tell the user that there is a problem with the template."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020076
Armin Ronacher7f15ef82008-05-16 09:11:39 +020077 def __init__(self, message, lineno, name=None, filename=None):
Armin Ronacher1d021082009-02-19 20:07:13 +010078 TemplateError.__init__(self, message)
Armin Ronacher07bc6842008-03-31 14:18:49 +020079 self.lineno = lineno
Armin Ronacher7f15ef82008-05-16 09:11:39 +020080 self.name = name
Armin Ronacheraaf010d2008-05-01 13:14:30 +020081 self.filename = filename
Armin Ronacherccae0552008-10-05 23:08:58 +020082 self.source = None
Armin Ronacherccae0552008-10-05 23:08:58 +020083
Armin Ronacherd416a972009-02-24 22:58:00 +010084 # this is set to True if the debug.translate_syntax_error
85 # function translated the syntax error into a new traceback
86 self.translated = False
87
Armin Ronacher790b8a82010-02-10 00:05:46 +010088 def __str__(self):
Thomas Waldmanne0003552013-05-17 23:52:14 +020089 return six.text_type(self).encode('utf-8')
Armin Ronacher790b8a82010-02-10 00:05:46 +010090
Armin Ronacheracbd4082010-02-10 00:07:43 +010091 # unicode goes after __str__ because we configured 2to3 to rename
92 # __unicode__ to __str__. because the 2to3 tree is not designed to
93 # remove nodes from it, we leave the above __str__ around and let
94 # it override at runtime.
Armin Ronacherccae0552008-10-05 23:08:58 +020095 def __unicode__(self):
Armin Ronacherd416a972009-02-24 22:58:00 +010096 # for translated errors we only return the message
97 if self.translated:
Armin Ronacher0d242be2010-02-10 01:35:13 +010098 return self.message
Armin Ronacherd416a972009-02-24 22:58:00 +010099
100 # otherwise attach some stuff
Armin Ronacherccae0552008-10-05 23:08:58 +0200101 location = 'line %d' % self.lineno
102 name = self.filename or self.name
103 if name:
104 location = 'File "%s", %s' % (name, location)
105 lines = [self.message, ' ' + location]
106
107 # if the source is set, add the line to the output
108 if self.source is not None:
109 try:
110 line = self.source.splitlines()[self.lineno - 1]
111 except IndexError:
112 line = None
113 if line:
114 lines.append(' ' + line.strip())
115
116 return u'\n'.join(lines)
117
Armin Ronacher07bc6842008-03-31 14:18:49 +0200118
Armin Ronacher53042292008-04-26 18:30:19 +0200119class TemplateAssertionError(TemplateSyntaxError):
Armin Ronacher9a822052008-04-17 18:44:07 +0200120 """Like a template syntax error, but covers cases where something in the
121 template caused an error at compile time that wasn't necessarily caused
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200122 by a syntax error. However it's a direct subclass of
123 :exc:`TemplateSyntaxError` and has the same attributes.
Armin Ronacher9a822052008-04-17 18:44:07 +0200124 """
Armin Ronachere791c2a2008-04-07 18:39:54 +0200125
Armin Ronachere791c2a2008-04-07 18:39:54 +0200126
Armin Ronacher07bc6842008-03-31 14:18:49 +0200127class TemplateRuntimeError(TemplateError):
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200128 """A generic runtime error in the template engine. Under some situations
129 Jinja may raise this exception.
130 """
Benjamin Wieganda3152742008-04-28 18:07:52 +0200131
132
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200133class UndefinedError(TemplateRuntimeError):
134 """Raised if a template tries to operate on :class:`Undefined`."""
135
136
137class SecurityError(TemplateRuntimeError):
138 """Raised if a template tries to do something insecure if the
139 sandbox is enabled.
140 """
141
142
143class FilterArgumentError(TemplateRuntimeError):
Benjamin Wieganda3152742008-04-28 18:07:52 +0200144 """This error is raised if a filter was called with inappropriate
145 arguments
146 """