]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/jinja2/exceptions.py
4 from .runtime
import Undefined
7 class TemplateError(Exception):
8 """Baseclass for all template errors."""
10 def __init__(self
, message
: t
.Optional
[str] = None) -> None:
11 super().__init
__(message
)
14 def message(self
) -> t
.Optional
[str]:
15 return self
.args
[0] if self
.args
else None
18 class TemplateNotFound(IOError, LookupError, TemplateError
):
19 """Raised if a template does not exist.
21 .. versionchanged:: 2.11
22 If the given name is :class:`Undefined` and no message was
23 provided, an :exc:`UndefinedError` is raised.
26 # Silence the Python warning about message being deprecated since
27 # it's not valid here.
28 message
: t
.Optional
[str] = None
32 name
: t
.Optional
[t
.Union
[str, "Undefined"]],
33 message
: t
.Optional
[str] = None,
35 IOError.__init
__(self
, name
)
38 from .runtime
import Undefined
40 if isinstance(name
, Undefined
):
41 name
._fail
_with
_undefined
_error
()
45 self
.message
= message
47 self
.templates
= [name
]
49 def __str__(self
) -> str:
50 return str(self
.message
)
53 class TemplatesNotFound(TemplateNotFound
):
54 """Like :class:`TemplateNotFound` but raised if multiple templates
55 are selected. This is a subclass of :class:`TemplateNotFound`
56 exception, so just catching the base exception will catch both.
58 .. versionchanged:: 2.11
59 If a name in the list of names is :class:`Undefined`, a message
60 about it being undefined is shown rather than the empty string.
67 names
: t
.Sequence
[t
.Union
[str, "Undefined"]] = (),
68 message
: t
.Optional
[str] = None,
71 from .runtime
import Undefined
76 if isinstance(name
, Undefined
):
77 parts
.append(name
._undefined
_message
)
81 parts_str
= ", ".join(map(str, parts
))
82 message
= f
"none of the templates given were found: {parts_str}"
84 super().__init
__(names
[-1] if names
else None, message
)
85 self
.templates
= list(names
)
88 class TemplateSyntaxError(TemplateError
):
89 """Raised to tell the user that there is a problem with the template."""
95 name
: t
.Optional
[str] = None,
96 filename
: t
.Optional
[str] = None,
98 super().__init
__(message
)
101 self
.filename
= filename
102 self
.source
: t
.Optional
[str] = None
104 # this is set to True if the debug.translate_syntax_error
105 # function translated the syntax error into a new traceback
106 self
.translated
= False
108 def __str__(self
) -> str:
109 # for translated errors we only return the message
111 return t
.cast(str, self
.message
)
113 # otherwise attach some stuff
114 location
= f
"line {self.lineno}"
115 name
= self
.filename
or self
.name
117 location
= f
'File "{name}", {location}'
118 lines
= [t
.cast(str, self
.message
), " " + location
]
120 # if the source is set, add the line to the output
121 if self
.source
is not None:
123 line
= self
.source
.splitlines()[self
.lineno
- 1]
127 lines
.append(" " + line
.strip())
129 return "\n".join(lines
)
131 def __reduce__(self
): # type: ignore
132 # https://bugs.python.org/issue1692335 Exceptions that take
133 # multiple required arguments have problems with pickling.
134 # Without this, raises TypeError: __init__() missing 1 required
135 # positional argument: 'lineno'
136 return self
.__class
__, (self
.message
, self
.lineno
, self
.name
, self
.filename
)
139 class TemplateAssertionError(TemplateSyntaxError
):
140 """Like a template syntax error, but covers cases where something in the
141 template caused an error at compile time that wasn't necessarily caused
142 by a syntax error. However it's a direct subclass of
143 :exc:`TemplateSyntaxError` and has the same attributes.
147 class TemplateRuntimeError(TemplateError
):
148 """A generic runtime error in the template engine. Under some situations
149 Jinja may raise this exception.
153 class UndefinedError(TemplateRuntimeError
):
154 """Raised if a template tries to operate on :class:`Undefined`."""
157 class SecurityError(TemplateRuntimeError
):
158 """Raised if a template tries to do something insecure if the
163 class FilterArgumentError(TemplateRuntimeError
):
164 """This error is raised if a filter was called with inappropriate