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