[PATCH in crew] templater: abort when a template filter raises an exception (issue2987)

Bryan O'Sullivan bos at serpentine.com
Tue Aug 21 15:13:41 CDT 2012


# HG changeset patch
# User Neil Kodner <neilk at fb.com>
# Date 1345241521 25200
# Node ID 099c778ceb33e26626fdc27eae362113b8894491
# Parent  c6f88e7f95b764e23b7e0b4353c5a6458bbc3cc4
templater: abort when a template filter raises an exception (issue2987)

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -146,7 +146,15 @@
 
 def runfilter(context, mapping, data):
     func, data, filt = data
-    return filt(func(context, mapping, data))
+    try:
+        return filt(func(context, mapping, data))
+    except (ValueError, AttributeError, TypeError):
+        if isinstance(data, tuple):
+            dt = data[1]
+        else:
+            dt = data
+        raise util.Abort(_("template filter '%s' is not compatible with "
+                           "keyword '%s'") % (filt.func_name, dt))
 
 def buildmap(exp, context):
     func, data = compileexp(exp[1], context)
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -1255,6 +1255,30 @@
   abort: t:3: unmatched quotes
   [255]
 
+Behind the scenes, this will throw TypeError
+
+  $ hg log -l 3 --template '{date|obfuscate}\n'
+  abort: Template filter 'obfuscate' is not compatible with keyword 'date'
+  [255]
+
+Behind the scenes, this will throw a ValueError
+
+  $ hg log -l 3 --template 'line: {desc|shortdate}\n'
+  abort: Template filter 'shortdate' is not compatible with keyword 'desc'
+  [255]
+
+Behind the scenes, this will throw AttributeError
+
+  $ hg log -l 3 --template 'line: {date|escape}\n'
+  abort: Template filter 'escape' is not compatible with keyword 'date'
+  [255]
+
+Behind the scenes, this will throw ValueError
+
+  $ hg tip --template '{author|email|date}\n'
+  abort: Template filter 'datefilter' is not compatible with keyword 'author'
+  [255]
+
   $ cd ..
 
 


More information about the Mercurial-devel mailing list