| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| Armin Ronacher | 26c0f51 | 2008-04-17 11:16:01 +0200 | [diff] [blame] | 3 | jinja2.exceptions |
| 4 | ~~~~~~~~~~~~~~~~~ |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 5 | |
| 6 | Jinja exceptions. |
| 7 | |
| Armin Ronacher | 55494e4 | 2010-01-22 09:41:48 +0100 | [diff] [blame] | 8 | :copyright: (c) 2010 by the Jinja Team. |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 9 | :license: BSD, see LICENSE for more details. |
| 10 | """ |
| Alex Morega | e29c84c | 2013-05-18 15:35:46 +0300 | [diff] [blame] | 11 | import sys |
| Thomas Waldmann | e000355 | 2013-05-17 23:52:14 +0200 | [diff] [blame] | 12 | import six |
| Daniel Neuhäuser | d0708db | 2013-05-18 12:52:40 +0200 | [diff] [blame] | 13 | from six.moves import map |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 14 | |
| 15 | |
| Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 16 | class TemplateError(Exception): |
| Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 17 | """Baseclass for all template errors.""" |
| 18 | |
| Alex Morega | e29c84c | 2013-05-18 15:35:46 +0300 | [diff] [blame] | 19 | if sys.version_info[0] < 3: |
| 20 | def __init__(self, message=None): |
| Armin Ronacher | 1d02108 | 2009-02-19 20:07:13 +0100 | [diff] [blame] | 21 | if message is not None: |
| Alex Morega | e29c84c | 2013-05-18 15:35:46 +0300 | [diff] [blame] | 22 | message = six.text_type(message).encode('utf-8') |
| 23 | Exception.__init__(self, message) |
| 24 | |
| 25 | @property |
| 26 | def message(self): |
| 27 | if self.args: |
| 28 | message = self.args[0] |
| 29 | if message is not None: |
| 30 | return message.decode('utf-8', 'replace') |
| 31 | |
| 32 | else: |
| 33 | @property |
| 34 | def message(self): |
| 35 | if self.args: |
| 36 | message = self.args[0] |
| 37 | if message is not None: |
| 38 | return message |
| Armin Ronacher | 1d02108 | 2009-02-19 20:07:13 +0100 | [diff] [blame] | 39 | |
| Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 40 | |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 41 | class TemplateNotFound(IOError, LookupError, TemplateError): |
| Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 42 | """Raised if a template does not exist.""" |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 43 | |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 44 | # looks weird, but removes the warning descriptor that just |
| 45 | # bogusly warns us about message being deprecated |
| 46 | message = None |
| 47 | |
| 48 | def __init__(self, name, message=None): |
| 49 | IOError.__init__(self) |
| 50 | if message is None: |
| 51 | message = name |
| 52 | self.message = message |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 53 | self.name = name |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 54 | self.templates = [name] |
| 55 | |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 56 | def __str__(self): |
| 57 | return self.message.encode('utf-8') |
| 58 | |
| Armin Ronacher | acbd408 | 2010-02-10 00:07:43 +0100 | [diff] [blame] | 59 | # unicode goes after __str__ because we configured 2to3 to rename |
| 60 | # __unicode__ to __str__. because the 2to3 tree is not designed to |
| 61 | # remove nodes from it, we leave the above __str__ around and let |
| 62 | # it override at runtime. |
| Armin Ronacher | 790b8a8 | 2010-02-10 00:05:46 +0100 | [diff] [blame] | 63 | def __unicode__(self): |
| 64 | return self.message |
| 65 | |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 66 | |
| 67 | class TemplatesNotFound(TemplateNotFound): |
| 68 | """Like :class:`TemplateNotFound` but raised if multiple templates |
| 69 | are selected. This is a subclass of :class:`TemplateNotFound` |
| 70 | exception, so just catching the base exception will catch both. |
| 71 | |
| 72 | .. versionadded:: 2.2 |
| 73 | """ |
| 74 | |
| 75 | def __init__(self, names=(), message=None): |
| 76 | if message is None: |
| Simon Sapin | 52c88fb | 2011-12-03 13:05:08 +0100 | [diff] [blame] | 77 | message = u'none of the templates given were found: ' + \ |
| Thomas Waldmann | 7d29562 | 2013-05-18 00:06:22 +0200 | [diff] [blame] | 78 | u', '.join(map(six.text_type, names)) |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 79 | TemplateNotFound.__init__(self, names and names[-1] or None, message) |
| 80 | self.templates = list(names) |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 81 | |
| 82 | |
| Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 83 | class TemplateSyntaxError(TemplateError): |
| Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 84 | """Raised to tell the user that there is a problem with the template.""" |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 85 | |
| Armin Ronacher | 7f15ef8 | 2008-05-16 09:11:39 +0200 | [diff] [blame] | 86 | def __init__(self, message, lineno, name=None, filename=None): |
| Armin Ronacher | 1d02108 | 2009-02-19 20:07:13 +0100 | [diff] [blame] | 87 | TemplateError.__init__(self, message) |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 88 | self.lineno = lineno |
| Armin Ronacher | 7f15ef8 | 2008-05-16 09:11:39 +0200 | [diff] [blame] | 89 | self.name = name |
| Armin Ronacher | aaf010d | 2008-05-01 13:14:30 +0200 | [diff] [blame] | 90 | self.filename = filename |
| Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 91 | self.source = None |
| Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 92 | |
| Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 93 | # this is set to True if the debug.translate_syntax_error |
| 94 | # function translated the syntax error into a new traceback |
| 95 | self.translated = False |
| 96 | |
| Armin Ronacher | 790b8a8 | 2010-02-10 00:05:46 +0100 | [diff] [blame] | 97 | def __str__(self): |
| Thomas Waldmann | 7d29562 | 2013-05-18 00:06:22 +0200 | [diff] [blame] | 98 | s = self.__unicode__() |
| 99 | return s if six.PY3 else s.encode('utf-8') |
| Armin Ronacher | 790b8a8 | 2010-02-10 00:05:46 +0100 | [diff] [blame] | 100 | |
| Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 101 | def __unicode__(self): |
| Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 102 | # for translated errors we only return the message |
| 103 | if self.translated: |
| Armin Ronacher | 0d242be | 2010-02-10 01:35:13 +0100 | [diff] [blame] | 104 | return self.message |
| Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 105 | |
| 106 | # otherwise attach some stuff |
| Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 107 | location = 'line %d' % self.lineno |
| 108 | name = self.filename or self.name |
| 109 | if name: |
| 110 | location = 'File "%s", %s' % (name, location) |
| 111 | lines = [self.message, ' ' + location] |
| 112 | |
| 113 | # if the source is set, add the line to the output |
| 114 | if self.source is not None: |
| 115 | try: |
| 116 | line = self.source.splitlines()[self.lineno - 1] |
| 117 | except IndexError: |
| 118 | line = None |
| 119 | if line: |
| 120 | lines.append(' ' + line.strip()) |
| 121 | |
| 122 | return u'\n'.join(lines) |
| 123 | |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 124 | |
| Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 125 | class TemplateAssertionError(TemplateSyntaxError): |
| Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 126 | """Like a template syntax error, but covers cases where something in the |
| 127 | template caused an error at compile time that wasn't necessarily caused |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 128 | by a syntax error. However it's a direct subclass of |
| 129 | :exc:`TemplateSyntaxError` and has the same attributes. |
| Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 130 | """ |
| Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 131 | |
| Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 132 | |
| Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 133 | class TemplateRuntimeError(TemplateError): |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 134 | """A generic runtime error in the template engine. Under some situations |
| 135 | Jinja may raise this exception. |
| 136 | """ |
| Benjamin Wiegand | a315274 | 2008-04-28 18:07:52 +0200 | [diff] [blame] | 137 | |
| 138 | |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 139 | class UndefinedError(TemplateRuntimeError): |
| 140 | """Raised if a template tries to operate on :class:`Undefined`.""" |
| 141 | |
| 142 | |
| 143 | class SecurityError(TemplateRuntimeError): |
| 144 | """Raised if a template tries to do something insecure if the |
| 145 | sandbox is enabled. |
| 146 | """ |
| 147 | |
| 148 | |
| 149 | class FilterArgumentError(TemplateRuntimeError): |
| Benjamin Wiegand | a315274 | 2008-04-28 18:07:52 +0200 | [diff] [blame] | 150 | """This error is raised if a filter was called with inappropriate |
| 151 | arguments |
| 152 | """ |