blob: 8311cf3dddf63dc5d054b245b8b0142f8a539408 [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 Ronacher62ccd1b2009-01-04 14:26:19 +01008 :copyright: (c) 2009 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
32 def __init__(self, name):
33 IOError.__init__(self, name)
34 self.name = name
35
36
Armin Ronacher68f77672008-04-17 11:50:39 +020037class TemplateSyntaxError(TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020038 """Raised to tell the user that there is a problem with the template."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020039
Armin Ronacher7f15ef82008-05-16 09:11:39 +020040 def __init__(self, message, lineno, name=None, filename=None):
Armin Ronacher1d021082009-02-19 20:07:13 +010041 TemplateError.__init__(self, message)
Armin Ronacher07bc6842008-03-31 14:18:49 +020042 self.lineno = lineno
Armin Ronacher7f15ef82008-05-16 09:11:39 +020043 self.name = name
Armin Ronacheraaf010d2008-05-01 13:14:30 +020044 self.filename = filename
Armin Ronacherccae0552008-10-05 23:08:58 +020045 self.source = None
Armin Ronacherccae0552008-10-05 23:08:58 +020046
47 def __unicode__(self):
48 location = 'line %d' % self.lineno
49 name = self.filename or self.name
50 if name:
51 location = 'File "%s", %s' % (name, location)
52 lines = [self.message, ' ' + location]
53
54 # if the source is set, add the line to the output
55 if self.source is not None:
56 try:
57 line = self.source.splitlines()[self.lineno - 1]
58 except IndexError:
59 line = None
60 if line:
61 lines.append(' ' + line.strip())
62
63 return u'\n'.join(lines)
64
65 def __str__(self):
66 return unicode(self).encode('utf-8')
Armin Ronacher07bc6842008-03-31 14:18:49 +020067
68
Armin Ronacher53042292008-04-26 18:30:19 +020069class TemplateAssertionError(TemplateSyntaxError):
Armin Ronacher9a822052008-04-17 18:44:07 +020070 """Like a template syntax error, but covers cases where something in the
71 template caused an error at compile time that wasn't necessarily caused
Armin Ronacherf3c35c42008-05-23 23:18:14 +020072 by a syntax error. However it's a direct subclass of
73 :exc:`TemplateSyntaxError` and has the same attributes.
Armin Ronacher9a822052008-04-17 18:44:07 +020074 """
Armin Ronachere791c2a2008-04-07 18:39:54 +020075
Armin Ronachere791c2a2008-04-07 18:39:54 +020076
Armin Ronacher07bc6842008-03-31 14:18:49 +020077class TemplateRuntimeError(TemplateError):
Armin Ronacherf3c35c42008-05-23 23:18:14 +020078 """A generic runtime error in the template engine. Under some situations
79 Jinja may raise this exception.
80 """
Benjamin Wieganda3152742008-04-28 18:07:52 +020081
82
Armin Ronacherf3c35c42008-05-23 23:18:14 +020083class UndefinedError(TemplateRuntimeError):
84 """Raised if a template tries to operate on :class:`Undefined`."""
85
86
87class SecurityError(TemplateRuntimeError):
88 """Raised if a template tries to do something insecure if the
89 sandbox is enabled.
90 """
91
92
93class FilterArgumentError(TemplateRuntimeError):
Benjamin Wieganda3152742008-04-28 18:07:52 +020094 """This error is raised if a filter was called with inappropriate
95 arguments
96 """