You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.3 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.exceptions
  4. ~~~~~~~~~~~~~~~~~
  5. Jinja exceptions.
  6. :copyright: (c) 2017 by the Jinja Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from jinja2._compat import imap, text_type, PY2, implements_to_string
  10. class TemplateError(Exception):
  11. """Baseclass for all template errors."""
  12. if PY2:
  13. def __init__(self, message=None):
  14. if message is not None:
  15. message = text_type(message).encode('utf-8')
  16. Exception.__init__(self, message)
  17. @property
  18. def message(self):
  19. if self.args:
  20. message = self.args[0]
  21. if message is not None:
  22. return message.decode('utf-8', 'replace')
  23. def __unicode__(self):
  24. return self.message or u''
  25. else:
  26. def __init__(self, message=None):
  27. Exception.__init__(self, message)
  28. @property
  29. def message(self):
  30. if self.args:
  31. message = self.args[0]
  32. if message is not None:
  33. return message
  34. @implements_to_string
  35. class TemplateNotFound(IOError, LookupError, TemplateError):
  36. """Raised if a template does not exist."""
  37. # looks weird, but removes the warning descriptor that just
  38. # bogusly warns us about message being deprecated
  39. message = None
  40. def __init__(self, name, message=None):
  41. IOError.__init__(self)
  42. if message is None:
  43. message = name
  44. self.message = message
  45. self.name = name
  46. self.templates = [name]
  47. def __str__(self):
  48. return self.message
  49. class TemplatesNotFound(TemplateNotFound):
  50. """Like :class:`TemplateNotFound` but raised if multiple templates
  51. are selected. This is a subclass of :class:`TemplateNotFound`
  52. exception, so just catching the base exception will catch both.
  53. .. versionadded:: 2.2
  54. """
  55. def __init__(self, names=(), message=None):
  56. if message is None:
  57. message = u'none of the templates given were found: ' + \
  58. u', '.join(imap(text_type, names))
  59. TemplateNotFound.__init__(self, names and names[-1] or None, message)
  60. self.templates = list(names)
  61. @implements_to_string
  62. class TemplateSyntaxError(TemplateError):
  63. """Raised to tell the user that there is a problem with the template."""
  64. def __init__(self, message, lineno, name=None, filename=None):
  65. TemplateError.__init__(self, message)
  66. self.lineno = lineno
  67. self.name = name
  68. self.filename = filename
  69. self.source = None
  70. # this is set to True if the debug.translate_syntax_error
  71. # function translated the syntax error into a new traceback
  72. self.translated = False
  73. def __str__(self):
  74. # for translated errors we only return the message
  75. if self.translated:
  76. return self.message
  77. # otherwise attach some stuff
  78. location = 'line %d' % self.lineno
  79. name = self.filename or self.name
  80. if name:
  81. location = 'File "%s", %s' % (name, location)
  82. lines = [self.message, ' ' + location]
  83. # if the source is set, add the line to the output
  84. if self.source is not None:
  85. try:
  86. line = self.source.splitlines()[self.lineno - 1]
  87. except IndexError:
  88. line = None
  89. if line:
  90. lines.append(' ' + line.strip())
  91. return u'\n'.join(lines)
  92. class TemplateAssertionError(TemplateSyntaxError):
  93. """Like a template syntax error, but covers cases where something in the
  94. template caused an error at compile time that wasn't necessarily caused
  95. by a syntax error. However it's a direct subclass of
  96. :exc:`TemplateSyntaxError` and has the same attributes.
  97. """
  98. class TemplateRuntimeError(TemplateError):
  99. """A generic runtime error in the template engine. Under some situations
  100. Jinja may raise this exception.
  101. """
  102. class UndefinedError(TemplateRuntimeError):
  103. """Raised if a template tries to operate on :class:`Undefined`."""
  104. class SecurityError(TemplateRuntimeError):
  105. """Raised if a template tries to do something insecure if the
  106. sandbox is enabled.
  107. """
  108. class FilterArgumentError(TemplateRuntimeError):
  109. """This error is raised if a filter was called with inappropriate
  110. arguments
  111. """