]> Devi Nivas Git - chevron.git/commitdiff
add custom exception: ChevronError
authorSamuel Colvin <s@muelcolvin.com>
Mon, 19 Jun 2017 16:15:36 +0000 (17:15 +0100)
committerSamuel Colvin <s@muelcolvin.com>
Mon, 19 Jun 2017 16:28:33 +0000 (17:28 +0100)
chevron/__init__.py
chevron/tokenizer.py
test_spec.py

index 0d4ad77ece0af5544f4d44df79e8397f9fd901ad..d34cdac2159d1346c40b4177c8a645a8458ed2b8 100644 (file)
@@ -1,4 +1,5 @@
 from .main import main, cli_main
 from .renderer import render
+from .tokenizer import ChevronError
 
-__all__ = ['main', 'render', 'cli_main']
+__all__ = ['main', 'render', 'cli_main', 'ChevronError']
index 44f4eac93b63cc7d65ce39bd3d735f2f36fda43e..ea3f6b2b0df15bfd4a40ad66a982ea170b3e9ca9 100644 (file)
@@ -3,10 +3,14 @@ _CURRENT_LINE = 1
 _LAST_TAG_LINE = None
 
 
+class ChevronError(SyntaxError):
+    pass
+
 #
 # Helper functions
 #
 
+
 def grab_literal(template, l_del):
     """Parse a literal from the template"""
 
@@ -95,8 +99,8 @@ def parse_tag(template, l_del, r_del):
 
         # Otherwise we should complain
         else:
-            raise SyntaxError('unclosed set delimiter tag\n'
-                              'at line {0}'.format(_CURRENT_LINE))
+            raise ChevronError('unclosed set delimiter tag\n'
+                               'at line {0}'.format(_CURRENT_LINE))
 
     # If we might be a no html escape tag
     elif tag_type == 'no escape?':
@@ -200,11 +204,11 @@ def tokenize(template, def_ldel='{{', def_rdel='}}'):
             last_section = open_sections.pop()
             if tag_key != last_section:
                 # Otherwise we need to complain
-                raise SyntaxError('Trying to close tag "{0}"\n'
-                                  'last open tag is "{1}"\n'
-                                  'line {2}'
-                                  .format(tag_key, last_section,
-                                          _CURRENT_LINE + 1))
+                raise ChevronError('Trying to close tag "{0}"\n'
+                                   'last open tag is "{1}"\n'
+                                   'line {2}'
+                                   .format(tag_key, last_section,
+                                           _CURRENT_LINE + 1))
 
         # Do the second check to see if we're a standalone
         is_standalone = r_sa_check(template, tag_type, is_standalone)
@@ -231,7 +235,7 @@ def tokenize(template, def_ldel='{{', def_rdel='}}'):
     # If there are any open sections when we're done
     if open_sections:
         # Then we need to complain
-        raise SyntaxError('Unexpected EOF\n'
-                          'the tag "{0}" was never closed\n'
-                          'was opened at line {1}'
-                          .format(open_sections[-1], _LAST_TAG_LINE))
+        raise ChevronError('Unexpected EOF\n'
+                           'the tag "{0}" was never closed\n'
+                           'was opened at line {1}'
+                           .format(open_sections[-1], _LAST_TAG_LINE))
index 8d1c662ef719f44d5d5b6f5d61d53ce7a4d7ab5b..4fb5d146f615e37bb18bbda37a4336fef0488c88 100755 (executable)
@@ -61,8 +61,10 @@ class ExpandedCoverage(unittest.TestCase):
             'template': '{{# section }} end of file'
         }
 
+        self.assertRaises(chevron.ChevronError, chevron.render, **test1)
+        self.assertRaises(chevron.ChevronError, chevron.render, **test2)
+        # check SyntaxError still catches ChevronError:
         self.assertRaises(SyntaxError, chevron.render, **test1)
-        self.assertRaises(SyntaxError, chevron.render, **test2)
 
     def test_bad_set_delimiter_tag(self):
         args = {