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