blob: 182c0619ddc8ce71358d237d179e1cb83414a5a4 [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
Armin Ronacherd416a972009-02-24 22:58:00 +010047 # 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
50
Armin Ronacherccae0552008-10-05 23:08:58 +020051 def __unicode__(self):
Armin Ronacherd416a972009-02-24 22:58:00 +010052 # for translated errors we only return the message
53 if self.translated:
54 return self.message.encode('utf-8')
55
56 # otherwise attach some stuff
Armin Ronacherccae0552008-10-05 23:08:58 +020057 location = 'line %d' % self.lineno
58 name = self.filename or self.name
59 if name:
60 location = 'File "%s", %s' % (name, location)
61 lines = [self.message, ' ' + location]
62
63 # if the source is set, add the line to the output
64 if self.source is not None:
65 try:
66 line = self.source.splitlines()[self.lineno - 1]
67 except IndexError:
68 line = None
69 if line:
70 lines.append(' ' + line.strip())
71
72 return u'\n'.join(lines)
73
74 def __str__(self):
75 return unicode(self).encode('utf-8')
Armin Ronacher07bc6842008-03-31 14:18:49 +020076
77
Armin Ronacher53042292008-04-26 18:30:19 +020078class TemplateAssertionError(TemplateSyntaxError):
Armin Ronacher9a822052008-04-17 18:44:07 +020079 """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
Armin Ronacherf3c35c42008-05-23 23:18:14 +020081 by a syntax error. However it's a direct subclass of
82 :exc:`TemplateSyntaxError` and has the same attributes.
Armin Ronacher9a822052008-04-17 18:44:07 +020083 """
Armin Ronachere791c2a2008-04-07 18:39:54 +020084
Armin Ronachere791c2a2008-04-07 18:39:54 +020085
Armin Ronacher07bc6842008-03-31 14:18:49 +020086class TemplateRuntimeError(TemplateError):
Armin Ronacherf3c35c42008-05-23 23:18:14 +020087 """A generic runtime error in the template engine. Under some situations
88 Jinja may raise this exception.
89 """
Benjamin Wieganda3152742008-04-28 18:07:52 +020090
91
Armin Ronacherf3c35c42008-05-23 23:18:14 +020092class UndefinedError(TemplateRuntimeError):
93 """Raised if a template tries to operate on :class:`Undefined`."""
94
95
96class SecurityError(TemplateRuntimeError):
97 """Raised if a template tries to do something insecure if the
98 sandbox is enabled.
99 """
100
101
102class FilterArgumentError(TemplateRuntimeError):
Benjamin Wieganda3152742008-04-28 18:07:52 +0200103 """This error is raised if a filter was called with inappropriate
104 arguments
105 """