blob: 813b7fb01489a40dc61b2bd971103fc28557cc75 [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
Daniel Neuhäuserd0708db2013-05-18 12:52:40 +020012from six.moves import map
Armin Ronacher07bc6842008-03-31 14:18:49 +020013
14
Armin Ronachere791c2a2008-04-07 18:39:54 +020015class TemplateError(Exception):
Armin Ronacher9a822052008-04-17 18:44:07 +020016 """Baseclass for all template errors."""
17
Armin Ronacher1d021082009-02-19 20:07:13 +010018 def __init__(self, message=None):
19 if message is not None:
Thomas Waldmanne0003552013-05-17 23:52:14 +020020 message = six.text_type(message).encode('utf-8')
Armin Ronacher1d021082009-02-19 20:07:13 +010021 Exception.__init__(self, message)
22
23 @property
24 def message(self):
25 if self.args:
26 message = self.args[0]
27 if message is not None:
28 return message.decode('utf-8', 'replace')
29
Armin Ronacher9a822052008-04-17 18:44:07 +020030
Armin Ronacher07bc6842008-03-31 14:18:49 +020031class TemplateNotFound(IOError, LookupError, TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020032 """Raised if a template does not exist."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020033
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010034 # looks weird, but removes the warning descriptor that just
35 # bogusly warns us about message being deprecated
36 message = None
37
38 def __init__(self, name, message=None):
39 IOError.__init__(self)
40 if message is None:
41 message = name
42 self.message = message
Armin Ronacher07bc6842008-03-31 14:18:49 +020043 self.name = name
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010044 self.templates = [name]
45
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010046 def __str__(self):
47 return self.message.encode('utf-8')
48
Armin Ronacheracbd4082010-02-10 00:07:43 +010049 # unicode goes after __str__ because we configured 2to3 to rename
50 # __unicode__ to __str__. because the 2to3 tree is not designed to
51 # remove nodes from it, we leave the above __str__ around and let
52 # it override at runtime.
Armin Ronacher790b8a82010-02-10 00:05:46 +010053 def __unicode__(self):
54 return self.message
55
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010056
57class TemplatesNotFound(TemplateNotFound):
58 """Like :class:`TemplateNotFound` but raised if multiple templates
59 are selected. This is a subclass of :class:`TemplateNotFound`
60 exception, so just catching the base exception will catch both.
61
62 .. versionadded:: 2.2
63 """
64
65 def __init__(self, names=(), message=None):
66 if message is None:
Simon Sapin52c88fb2011-12-03 13:05:08 +010067 message = u'none of the templates given were found: ' + \
Thomas Waldmann7d295622013-05-18 00:06:22 +020068 u', '.join(map(six.text_type, names))
Armin Ronacher31bbd9e2010-01-14 00:41:30 +010069 TemplateNotFound.__init__(self, names and names[-1] or None, message)
70 self.templates = list(names)
Armin Ronacher07bc6842008-03-31 14:18:49 +020071
72
Armin Ronacher68f77672008-04-17 11:50:39 +020073class TemplateSyntaxError(TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020074 """Raised to tell the user that there is a problem with the template."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020075
Armin Ronacher7f15ef82008-05-16 09:11:39 +020076 def __init__(self, message, lineno, name=None, filename=None):
Armin Ronacher1d021082009-02-19 20:07:13 +010077 TemplateError.__init__(self, message)
Armin Ronacher07bc6842008-03-31 14:18:49 +020078 self.lineno = lineno
Armin Ronacher7f15ef82008-05-16 09:11:39 +020079 self.name = name
Armin Ronacheraaf010d2008-05-01 13:14:30 +020080 self.filename = filename
Armin Ronacherccae0552008-10-05 23:08:58 +020081 self.source = None
Armin Ronacherccae0552008-10-05 23:08:58 +020082
Armin Ronacherd416a972009-02-24 22:58:00 +010083 # this is set to True if the debug.translate_syntax_error
84 # function translated the syntax error into a new traceback
85 self.translated = False
86
Armin Ronacher790b8a82010-02-10 00:05:46 +010087 def __str__(self):
Thomas Waldmann7d295622013-05-18 00:06:22 +020088 s = self.__unicode__()
89 return s if six.PY3 else s.encode('utf-8')
Armin Ronacher790b8a82010-02-10 00:05:46 +010090
Armin Ronacherccae0552008-10-05 23:08:58 +020091 def __unicode__(self):
Armin Ronacherd416a972009-02-24 22:58:00 +010092 # for translated errors we only return the message
93 if self.translated:
Armin Ronacher0d242be2010-02-10 01:35:13 +010094 return self.message
Armin Ronacherd416a972009-02-24 22:58:00 +010095
96 # otherwise attach some stuff
Armin Ronacherccae0552008-10-05 23:08:58 +020097 location = 'line %d' % self.lineno
98 name = self.filename or self.name
99 if name:
100 location = 'File "%s", %s' % (name, location)
101 lines = [self.message, ' ' + location]
102
103 # if the source is set, add the line to the output
104 if self.source is not None:
105 try:
106 line = self.source.splitlines()[self.lineno - 1]
107 except IndexError:
108 line = None
109 if line:
110 lines.append(' ' + line.strip())
111
112 return u'\n'.join(lines)
113
Armin Ronacher07bc6842008-03-31 14:18:49 +0200114
Armin Ronacher53042292008-04-26 18:30:19 +0200115class TemplateAssertionError(TemplateSyntaxError):
Armin Ronacher9a822052008-04-17 18:44:07 +0200116 """Like a template syntax error, but covers cases where something in the
117 template caused an error at compile time that wasn't necessarily caused
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200118 by a syntax error. However it's a direct subclass of
119 :exc:`TemplateSyntaxError` and has the same attributes.
Armin Ronacher9a822052008-04-17 18:44:07 +0200120 """
Armin Ronachere791c2a2008-04-07 18:39:54 +0200121
Armin Ronachere791c2a2008-04-07 18:39:54 +0200122
Armin Ronacher07bc6842008-03-31 14:18:49 +0200123class TemplateRuntimeError(TemplateError):
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200124 """A generic runtime error in the template engine. Under some situations
125 Jinja may raise this exception.
126 """
Benjamin Wieganda3152742008-04-28 18:07:52 +0200127
128
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200129class UndefinedError(TemplateRuntimeError):
130 """Raised if a template tries to operate on :class:`Undefined`."""
131
132
133class SecurityError(TemplateRuntimeError):
134 """Raised if a template tries to do something insecure if the
135 sandbox is enabled.
136 """
137
138
139class FilterArgumentError(TemplateRuntimeError):
Benjamin Wieganda3152742008-04-28 18:07:52 +0200140 """This error is raised if a filter was called with inappropriate
141 arguments
142 """