1class TemplateError(Exception): 2 """Baseclass for all template errors.""" 3 4 def __init__(self, message=None): 5 super().__init__(message) 6 7 @property 8 def message(self): 9 if self.args: 10 return self.args[0] 11 12 13class TemplateNotFound(IOError, LookupError, TemplateError): 14 """Raised if a template does not exist. 15 16 .. versionchanged:: 2.11 17 If the given name is :class:`Undefined` and no message was 18 provided, an :exc:`UndefinedError` is raised. 19 """ 20 21 # Silence the Python warning about message being deprecated since 22 # it's not valid here. 23 message = None 24 25 def __init__(self, name, message=None): 26 IOError.__init__(self, name) 27 28 if message is None: 29 from .runtime import Undefined 30 31 if isinstance(name, Undefined): 32 name._fail_with_undefined_error() 33 34 message = name 35 36 self.message = message 37 self.name = name 38 self.templates = [name] 39 40 def __str__(self): 41 return self.message 42 43 44class TemplatesNotFound(TemplateNotFound): 45 """Like :class:`TemplateNotFound` but raised if multiple templates 46 are selected. This is a subclass of :class:`TemplateNotFound` 47 exception, so just catching the base exception will catch both. 48 49 .. versionchanged:: 2.11 50 If a name in the list of names is :class:`Undefined`, a message 51 about it being undefined is shown rather than the empty string. 52 53 .. versionadded:: 2.2 54 """ 55 56 def __init__(self, names=(), message=None): 57 if message is None: 58 from .runtime import Undefined 59 60 parts = [] 61 62 for name in names: 63 if isinstance(name, Undefined): 64 parts.append(name._undefined_message) 65 else: 66 parts.append(name) 67 68 message = "none of the templates given were found: " + ", ".join( 69 map(str, parts) 70 ) 71 TemplateNotFound.__init__(self, names[-1] if names else None, message) 72 self.templates = list(names) 73 74 75class TemplateSyntaxError(TemplateError): 76 """Raised to tell the user that there is a problem with the template.""" 77 78 def __init__(self, message, lineno, name=None, filename=None): 79 TemplateError.__init__(self, message) 80 self.lineno = lineno 81 self.name = name 82 self.filename = filename 83 self.source = None 84 85 # this is set to True if the debug.translate_syntax_error 86 # function translated the syntax error into a new traceback 87 self.translated = False 88 89 def __str__(self): 90 # for translated errors we only return the message 91 if self.translated: 92 return self.message 93 94 # otherwise attach some stuff 95 location = f"line {self.lineno}" 96 name = self.filename or self.name 97 if name: 98 location = f'File "{name}", {location}' 99 lines = [self.message, " " + location] 100 101 # if the source is set, add the line to the output 102 if self.source is not None: 103 try: 104 line = self.source.splitlines()[self.lineno - 1] 105 except IndexError: 106 line = None 107 if line: 108 lines.append(" " + line.strip()) 109 110 return "\n".join(lines) 111 112 def __reduce__(self): 113 # https://bugs.python.org/issue1692335 Exceptions that take 114 # multiple required arguments have problems with pickling. 115 # Without this, raises TypeError: __init__() missing 1 required 116 # positional argument: 'lineno' 117 return self.__class__, (self.message, self.lineno, self.name, self.filename) 118 119 120class TemplateAssertionError(TemplateSyntaxError): 121 """Like a template syntax error, but covers cases where something in the 122 template caused an error at compile time that wasn't necessarily caused 123 by a syntax error. However it's a direct subclass of 124 :exc:`TemplateSyntaxError` and has the same attributes. 125 """ 126 127 128class TemplateRuntimeError(TemplateError): 129 """A generic runtime error in the template engine. Under some situations 130 Jinja may raise this exception. 131 """ 132 133 134class UndefinedError(TemplateRuntimeError): 135 """Raised if a template tries to operate on :class:`Undefined`.""" 136 137 138class SecurityError(TemplateRuntimeError): 139 """Raised if a template tries to do something insecure if the 140 sandbox is enabled. 141 """ 142 143 144class FilterArgumentError(TemplateRuntimeError): 145 """This error is raised if a filter was called with inappropriate 146 arguments 147 """ 148