| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 """ | 2 """ |
| 3 jinja2.exceptions | 3 jinja2.exceptions |
| 4 ~~~~~~~~~~~~~~~~~ | 4 ~~~~~~~~~~~~~~~~~ |
| 5 | 5 |
| 6 Jinja exceptions. | 6 Jinja exceptions. |
| 7 | 7 |
| 8 :copyright: (c) 2010 by the Jinja Team. | 8 :copyright: (c) 2010 by the Jinja Team. |
| 9 :license: BSD, see LICENSE for more details. | 9 :license: BSD, see LICENSE for more details. |
| 10 """ | 10 """ |
| 11 from jinja2._compat import imap, text_type, PY2, implements_to_string |
| 11 | 12 |
| 12 | 13 |
| 13 class TemplateError(Exception): | 14 class TemplateError(Exception): |
| 14 """Baseclass for all template errors.""" | 15 """Baseclass for all template errors.""" |
| 15 | 16 |
| 16 def __init__(self, message=None): | 17 if PY2: |
| 17 if message is not None: | 18 def __init__(self, message=None): |
| 18 message = unicode(message).encode('utf-8') | 19 if message is not None: |
| 19 Exception.__init__(self, message) | 20 message = text_type(message).encode('utf-8') |
| 21 Exception.__init__(self, message) |
| 20 | 22 |
| 21 @property | 23 @property |
| 22 def message(self): | 24 def message(self): |
| 23 if self.args: | 25 if self.args: |
| 24 message = self.args[0] | 26 message = self.args[0] |
| 25 if message is not None: | 27 if message is not None: |
| 26 return message.decode('utf-8', 'replace') | 28 return message.decode('utf-8', 'replace') |
| 29 |
| 30 def __unicode__(self): |
| 31 return self.message or u'' |
| 32 else: |
| 33 def __init__(self, message=None): |
| 34 Exception.__init__(self, message) |
| 35 |
| 36 @property |
| 37 def message(self): |
| 38 if self.args: |
| 39 message = self.args[0] |
| 40 if message is not None: |
| 41 return message |
| 27 | 42 |
| 28 | 43 |
| 44 @implements_to_string |
| 29 class TemplateNotFound(IOError, LookupError, TemplateError): | 45 class TemplateNotFound(IOError, LookupError, TemplateError): |
| 30 """Raised if a template does not exist.""" | 46 """Raised if a template does not exist.""" |
| 31 | 47 |
| 32 # looks weird, but removes the warning descriptor that just | 48 # looks weird, but removes the warning descriptor that just |
| 33 # bogusly warns us about message being deprecated | 49 # bogusly warns us about message being deprecated |
| 34 message = None | 50 message = None |
| 35 | 51 |
| 36 def __init__(self, name, message=None): | 52 def __init__(self, name, message=None): |
| 37 IOError.__init__(self) | 53 IOError.__init__(self) |
| 38 if message is None: | 54 if message is None: |
| 39 message = name | 55 message = name |
| 40 self.message = message | 56 self.message = message |
| 41 self.name = name | 57 self.name = name |
| 42 self.templates = [name] | 58 self.templates = [name] |
| 43 | 59 |
| 44 def __str__(self): | 60 def __str__(self): |
| 45 return self.message.encode('utf-8') | |
| 46 | |
| 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. | |
| 51 def __unicode__(self): | |
| 52 return self.message | 61 return self.message |
| 53 | 62 |
| 54 | 63 |
| 55 class TemplatesNotFound(TemplateNotFound): | 64 class TemplatesNotFound(TemplateNotFound): |
| 56 """Like :class:`TemplateNotFound` but raised if multiple templates | 65 """Like :class:`TemplateNotFound` but raised if multiple templates |
| 57 are selected. This is a subclass of :class:`TemplateNotFound` | 66 are selected. This is a subclass of :class:`TemplateNotFound` |
| 58 exception, so just catching the base exception will catch both. | 67 exception, so just catching the base exception will catch both. |
| 59 | 68 |
| 60 .. versionadded:: 2.2 | 69 .. versionadded:: 2.2 |
| 61 """ | 70 """ |
| 62 | 71 |
| 63 def __init__(self, names=(), message=None): | 72 def __init__(self, names=(), message=None): |
| 64 if message is None: | 73 if message is None: |
| 65 message = u'non of the templates given were found: ' + \ | 74 message = u'none of the templates given were found: ' + \ |
| 66 u', '.join(map(unicode, names)) | 75 u', '.join(imap(text_type, names)) |
| 67 TemplateNotFound.__init__(self, names and names[-1] or None, message) | 76 TemplateNotFound.__init__(self, names and names[-1] or None, message) |
| 68 self.templates = list(names) | 77 self.templates = list(names) |
| 69 | 78 |
| 70 | 79 |
| 80 @implements_to_string |
| 71 class TemplateSyntaxError(TemplateError): | 81 class TemplateSyntaxError(TemplateError): |
| 72 """Raised to tell the user that there is a problem with the template.""" | 82 """Raised to tell the user that there is a problem with the template.""" |
| 73 | 83 |
| 74 def __init__(self, message, lineno, name=None, filename=None): | 84 def __init__(self, message, lineno, name=None, filename=None): |
| 75 TemplateError.__init__(self, message) | 85 TemplateError.__init__(self, message) |
| 76 self.lineno = lineno | 86 self.lineno = lineno |
| 77 self.name = name | 87 self.name = name |
| 78 self.filename = filename | 88 self.filename = filename |
| 79 self.source = None | 89 self.source = None |
| 80 | 90 |
| 81 # this is set to True if the debug.translate_syntax_error | 91 # this is set to True if the debug.translate_syntax_error |
| 82 # function translated the syntax error into a new traceback | 92 # function translated the syntax error into a new traceback |
| 83 self.translated = False | 93 self.translated = False |
| 84 | 94 |
| 85 def __str__(self): | 95 def __str__(self): |
| 86 return unicode(self).encode('utf-8') | |
| 87 | |
| 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. | |
| 92 def __unicode__(self): | |
| 93 # for translated errors we only return the message | 96 # for translated errors we only return the message |
| 94 if self.translated: | 97 if self.translated: |
| 95 return self.message | 98 return self.message |
| 96 | 99 |
| 97 # otherwise attach some stuff | 100 # otherwise attach some stuff |
| 98 location = 'line %d' % self.lineno | 101 location = 'line %d' % self.lineno |
| 99 name = self.filename or self.name | 102 name = self.filename or self.name |
| 100 if name: | 103 if name: |
| 101 location = 'File "%s", %s' % (name, location) | 104 location = 'File "%s", %s' % (name, location) |
| 102 lines = [self.message, ' ' + location] | 105 lines = [self.message, ' ' + location] |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 class SecurityError(TemplateRuntimeError): | 137 class SecurityError(TemplateRuntimeError): |
| 135 """Raised if a template tries to do something insecure if the | 138 """Raised if a template tries to do something insecure if the |
| 136 sandbox is enabled. | 139 sandbox is enabled. |
| 137 """ | 140 """ |
| 138 | 141 |
| 139 | 142 |
| 140 class FilterArgumentError(TemplateRuntimeError): | 143 class FilterArgumentError(TemplateRuntimeError): |
| 141 """This error is raised if a filter was called with inappropriate | 144 """This error is raised if a filter was called with inappropriate |
| 142 arguments | 145 arguments |
| 143 """ | 146 """ |
| OLD | NEW |