blob: 154cf44c96df86d1e14f685e9f913025c9a0ebbc [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 Ronacherd1342312008-04-28 12:20:12 +02008 :copyright: 2008 by Armin Ronacher.
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
16
Armin Ronacher07bc6842008-03-31 14:18:49 +020017class TemplateNotFound(IOError, LookupError, TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020018 """Raised if a template does not exist."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020019
20 def __init__(self, name):
21 IOError.__init__(self, name)
22 self.name = name
23
24
Armin Ronacher68f77672008-04-17 11:50:39 +020025class TemplateSyntaxError(TemplateError):
Armin Ronacher9a822052008-04-17 18:44:07 +020026 """Raised to tell the user that there is a problem with the template."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020027
Armin Ronacher7f15ef82008-05-16 09:11:39 +020028 def __init__(self, message, lineno, name=None, filename=None):
29 if name is not None:
Armin Ronacherf3c35c42008-05-23 23:18:14 +020030 extra = '%s, line %d' % (name.encode('utf-8'), lineno)
Armin Ronacher7f15ef82008-05-16 09:11:39 +020031 else:
32 extra = 'line %d' % lineno
Armin Ronacherf3c35c42008-05-23 23:18:14 +020033 # if the message was provided as unicode we have to encode it
34 # to utf-8 explicitly
35 if isinstance(message, unicode):
36 message = message.encode('utf-8')
37 # otherwise make sure it's a in fact valid utf-8
38 else:
39 message = message.decode('utf-8', 'ignore').encode('utf-8')
Armin Ronacher7f15ef82008-05-16 09:11:39 +020040 TemplateError.__init__(self, '%s (%s)' % (message, extra))
Armin Ronachere791c2a2008-04-07 18:39:54 +020041 self.message = 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 Ronacher07bc6842008-03-31 14:18:49 +020045
46
Armin Ronacher53042292008-04-26 18:30:19 +020047class TemplateAssertionError(TemplateSyntaxError):
Armin Ronacher9a822052008-04-17 18:44:07 +020048 """Like a template syntax error, but covers cases where something in the
49 template caused an error at compile time that wasn't necessarily caused
Armin Ronacherf3c35c42008-05-23 23:18:14 +020050 by a syntax error. However it's a direct subclass of
51 :exc:`TemplateSyntaxError` and has the same attributes.
Armin Ronacher9a822052008-04-17 18:44:07 +020052 """
Armin Ronachere791c2a2008-04-07 18:39:54 +020053
Armin Ronachere791c2a2008-04-07 18:39:54 +020054
Armin Ronacher07bc6842008-03-31 14:18:49 +020055class TemplateRuntimeError(TemplateError):
Armin Ronacherf3c35c42008-05-23 23:18:14 +020056 """A generic runtime error in the template engine. Under some situations
57 Jinja may raise this exception.
58 """
Benjamin Wieganda3152742008-04-28 18:07:52 +020059
60
Armin Ronacherf3c35c42008-05-23 23:18:14 +020061class UndefinedError(TemplateRuntimeError):
62 """Raised if a template tries to operate on :class:`Undefined`."""
63
64
65class SecurityError(TemplateRuntimeError):
66 """Raised if a template tries to do something insecure if the
67 sandbox is enabled.
68 """
69
70
71class FilterArgumentError(TemplateRuntimeError):
Benjamin Wieganda3152742008-04-28 18:07:52 +020072 """This error is raised if a filter was called with inappropriate
73 arguments
74 """