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 | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 8 | :copyright: 2008 by Armin Ronacher. |
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 | |
| 16 | |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 17 | class TemplateNotFound(IOError, LookupError, TemplateError): |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 18 | """Raised if a template does not exist.""" |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 19 | |
| 20 | def __init__(self, name): |
| 21 | IOError.__init__(self, name) |
| 22 | self.name = name |
| 23 | |
| 24 | |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 25 | class TemplateSyntaxError(TemplateError): |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 26 | """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] | 27 | |
Armin Ronacher | 7f15ef8 | 2008-05-16 09:11:39 +0200 | [diff] [blame] | 28 | def __init__(self, message, lineno, name=None, filename=None): |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame^] | 29 | if not isinstance(message, unicode): |
| 30 | message = message.decode('utf-8', 'replace') |
| 31 | TemplateError.__init__(self, message.encode('utf-8')) |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 32 | self.lineno = lineno |
Armin Ronacher | 7f15ef8 | 2008-05-16 09:11:39 +0200 | [diff] [blame] | 33 | self.name = name |
Armin Ronacher | aaf010d | 2008-05-01 13:14:30 +0200 | [diff] [blame] | 34 | self.filename = filename |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame^] | 35 | self.source = None |
| 36 | self.message = message |
| 37 | |
| 38 | def __unicode__(self): |
| 39 | location = 'line %d' % self.lineno |
| 40 | name = self.filename or self.name |
| 41 | if name: |
| 42 | location = 'File "%s", %s' % (name, location) |
| 43 | lines = [self.message, ' ' + location] |
| 44 | |
| 45 | # if the source is set, add the line to the output |
| 46 | if self.source is not None: |
| 47 | try: |
| 48 | line = self.source.splitlines()[self.lineno - 1] |
| 49 | except IndexError: |
| 50 | line = None |
| 51 | if line: |
| 52 | lines.append(' ' + line.strip()) |
| 53 | |
| 54 | return u'\n'.join(lines) |
| 55 | |
| 56 | def __str__(self): |
| 57 | return unicode(self).encode('utf-8') |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 58 | |
| 59 | |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 60 | class TemplateAssertionError(TemplateSyntaxError): |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 61 | """Like a template syntax error, but covers cases where something in the |
| 62 | template caused an error at compile time that wasn't necessarily caused |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 63 | by a syntax error. However it's a direct subclass of |
| 64 | :exc:`TemplateSyntaxError` and has the same attributes. |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 65 | """ |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 66 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 67 | |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 68 | class TemplateRuntimeError(TemplateError): |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 69 | """A generic runtime error in the template engine. Under some situations |
| 70 | Jinja may raise this exception. |
| 71 | """ |
Benjamin Wiegand | a315274 | 2008-04-28 18:07:52 +0200 | [diff] [blame] | 72 | |
| 73 | |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 74 | class UndefinedError(TemplateRuntimeError): |
| 75 | """Raised if a template tries to operate on :class:`Undefined`.""" |
| 76 | |
| 77 | |
| 78 | class SecurityError(TemplateRuntimeError): |
| 79 | """Raised if a template tries to do something insecure if the |
| 80 | sandbox is enabled. |
| 81 | """ |
| 82 | |
| 83 | |
| 84 | class FilterArgumentError(TemplateRuntimeError): |
Benjamin Wiegand | a315274 | 2008-04-28 18:07:52 +0200 | [diff] [blame] | 85 | """This error is raised if a filter was called with inappropriate |
| 86 | arguments |
| 87 | """ |