Broken overlong line.
[jinja2.git] / jinja2 / exceptions.py
1 # -*- coding: utf-8 -*-
2 """
3     jinja2.exceptions
4     ~~~~~~~~~~~~~~~~~
5
6     Jinja exceptions.
7
8     :copyright: (c) 2009 by the Jinja Team.
9     :license: BSD, see LICENSE for more details.
10 """
11
12
13 class TemplateError(Exception):
14     """Baseclass for all template errors."""
15
16     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
28
29 class TemplateNotFound(IOError, LookupError, TemplateError):
30     """Raised if a template does not exist."""
31
32     def __init__(self, name):
33         IOError.__init__(self, name)
34         self.name = name
35
36
37 class TemplateSyntaxError(TemplateError):
38     """Raised to tell the user that there is a problem with the template."""
39
40     def __init__(self, message, lineno, name=None, filename=None):
41         TemplateError.__init__(self, message)
42         self.lineno = lineno
43         self.name = name
44         self.filename = filename
45         self.source = None
46
47         # 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
51     def __unicode__(self):
52         # 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
57         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')
76
77
78 class TemplateAssertionError(TemplateSyntaxError):
79     """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
81     by a syntax error.  However it's a direct subclass of
82     :exc:`TemplateSyntaxError` and has the same attributes.
83     """
84
85
86 class TemplateRuntimeError(TemplateError):
87     """A generic runtime error in the template engine.  Under some situations
88     Jinja may raise this exception.
89     """
90
91
92 class UndefinedError(TemplateRuntimeError):
93     """Raised if a template tries to operate on :class:`Undefined`."""
94
95
96 class SecurityError(TemplateRuntimeError):
97     """Raised if a template tries to do something insecure if the
98     sandbox is enabled.
99     """
100
101
102 class FilterArgumentError(TemplateRuntimeError):
103     """This error is raised if a filter was called with inappropriate
104     arguments
105     """