Skip to content
Myr

exceptions.py

This file

Path

jinja2/exceptions.py

143 lines · 4.4 KiB · Python

Last changed by

Down to 7 failures for Python 3. We're onto something.Armin Ronacherbefore adoption0d242be

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    jinja2.exceptions4    ~~~~~~~~~~~~~~~~~56    Jinja exceptions.78    :copyright: (c) 2010 by the Jinja Team.9    :license: BSD, see LICENSE for more details.10"""111213class TemplateError(Exception):14    """Baseclass for all template errors."""1516    def __init__(self, message=None):17        if message is not None:18            message = unicode(message).encode('utf-8')19        Exception.__init__(self, message)2021    @property22    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')272829class TemplateNotFound(IOError, LookupError, TemplateError):30    """Raised if a template does not exist."""3132    # looks weird, but removes the warning descriptor that just33    # bogusly warns us about message being deprecated34    message = None3536    def __init__(self, name, message=None):37        IOError.__init__(self)38        if message is None:39            message = name40        self.message = message41        self.name = name42        self.templates = [name]4344    def __str__(self):45        return self.message.encode('utf-8')4647    # unicode goes after __str__ because we configured 2to3 to rename48    # __unicode__ to __str__.  because the 2to3 tree is not designed to49    # remove nodes from it, we leave the above __str__ around and let50    # it override at runtime.51    def __unicode__(self):52        return self.message535455class TemplatesNotFound(TemplateNotFound):56    """Like :class:`TemplateNotFound` but raised if multiple templates57    are selected.  This is a subclass of :class:`TemplateNotFound`58    exception, so just catching the base exception will catch both.5960    .. versionadded:: 2.261    """6263    def __init__(self, names=(), message=None):64        if message is None:65            message = u'non of the templates given were found: ' + \66                      u', '.join(map(unicode, names))67        TemplateNotFound.__init__(self, names and names[-1] or None, message)68        self.templates = list(names)697071class TemplateSyntaxError(TemplateError):72    """Raised to tell the user that there is a problem with the template."""7374    def __init__(self, message, lineno, name=None, filename=None):75        TemplateError.__init__(self, message)76        self.lineno = lineno77        self.name = name78        self.filename = filename79        self.source = None8081        # this is set to True if the debug.translate_syntax_error82        # function translated the syntax error into a new traceback83        self.translated = False8485    def __str__(self):86        return unicode(self).encode('utf-8')8788    # unicode goes after __str__ because we configured 2to3 to rename89    # __unicode__ to __str__.  because the 2to3 tree is not designed to90    # remove nodes from it, we leave the above __str__ around and let91    # it override at runtime.92    def __unicode__(self):93        # for translated errors we only return the message94        if self.translated:95            return self.message9697        # otherwise attach some stuff98        location = 'line %d' % self.lineno99        name = self.filename or self.name100        if name:101            location = 'File "%s", %s' % (name, location)102        lines = [self.message, '  ' + location]103104        # if the source is set, add the line to the output105        if self.source is not None:106            try:107                line = self.source.splitlines()[self.lineno - 1]108            except IndexError:109                line = None110            if line:111                lines.append('    ' + line.strip())112113        return u'\n'.join(lines)114115116class TemplateAssertionError(TemplateSyntaxError):117    """Like a template syntax error, but covers cases where something in the118    template caused an error at compile time that wasn't necessarily caused119    by a syntax error.  However it's a direct subclass of120    :exc:`TemplateSyntaxError` and has the same attributes.121    """122123124class TemplateRuntimeError(TemplateError):125    """A generic runtime error in the template engine.  Under some situations126    Jinja may raise this exception.127    """128129130class UndefinedError(TemplateRuntimeError):131    """Raised if a template tries to operate on :class:`Undefined`."""132133134class SecurityError(TemplateRuntimeError):135    """Raised if a template tries to do something insecure if the136    sandbox is enabled.137    """138139140class FilterArgumentError(TemplateRuntimeError):141    """This error is raised if a filter was called with inappropriate142    arguments143    """