With number of course. Jinja2.pdf not Jinja.pdf
[jinja2.git] / jinja2 / exceptions.py
index 182c0619ddc8ce71358d237d179e1cb83414a5a4..771f6a8d7a763e9bf2201ff9511dc25611ca7456 100644 (file)
@@ -5,7 +5,7 @@
 
     Jinja exceptions.
 
-    :copyright: (c) 2009 by the Jinja Team.
+    :copyright: (c) 2010 by the Jinja Team.
     :license: BSD, see LICENSE for more details.
 """
 
@@ -29,9 +29,43 @@ class TemplateError(Exception):
 class TemplateNotFound(IOError, LookupError, TemplateError):
     """Raised if a template does not exist."""
 
-    def __init__(self, name):
-        IOError.__init__(self, name)
+    # looks weird, but removes the warning descriptor that just
+    # bogusly warns us about message being deprecated
+    message = None
+
+    def __init__(self, name, message=None):
+        IOError.__init__(self)
+        if message is None:
+            message = name
+        self.message = message
         self.name = name
+        self.templates = [name]
+
+    def __str__(self):
+        return self.message.encode('utf-8')
+
+    # unicode goes after __str__ because we configured 2to3 to rename
+    # __unicode__ to __str__.  because the 2to3 tree is not designed to
+    # remove nodes from it, we leave the above __str__ around and let
+    # it override at runtime.
+    def __unicode__(self):
+        return self.message
+
+
+class TemplatesNotFound(TemplateNotFound):
+    """Like :class:`TemplateNotFound` but raised if multiple templates
+    are selected.  This is a subclass of :class:`TemplateNotFound`
+    exception, so just catching the base exception will catch both.
+
+    .. versionadded:: 2.2
+    """
+
+    def __init__(self, names=(), message=None):
+        if message is None:
+            message = u'non of the templates given were found: ' + \
+                      u', '.join(map(unicode, names))
+        TemplateNotFound.__init__(self, names and names[-1] or None, message)
+        self.templates = list(names)
 
 
 class TemplateSyntaxError(TemplateError):
@@ -48,10 +82,17 @@ class TemplateSyntaxError(TemplateError):
         # function translated the syntax error into a new traceback
         self.translated = False
 
+    def __str__(self):
+        return unicode(self).encode('utf-8')
+
+    # unicode goes after __str__ because we configured 2to3 to rename
+    # __unicode__ to __str__.  because the 2to3 tree is not designed to
+    # remove nodes from it, we leave the above __str__ around and let
+    # it override at runtime.
     def __unicode__(self):
         # for translated errors we only return the message
         if self.translated:
-            return self.message.encode('utf-8')
+            return self.message
 
         # otherwise attach some stuff
         location = 'line %d' % self.lineno
@@ -71,9 +112,6 @@ class TemplateSyntaxError(TemplateError):
 
         return u'\n'.join(lines)
 
-    def __str__(self):
-        return unicode(self).encode('utf-8')
-
 
 class TemplateAssertionError(TemplateSyntaxError):
     """Like a template syntax error, but covers cases where something in the