Patchwork [1,of,2] templater: give better error message for invalid engine type

login
register
mail settings
Submitter Yuya Nishihara
Date April 8, 2016, 3:57 p.m.
Message ID <170ea5eb48ccdd68010c.1460131024@mimosa>
Download mbox | patch
Permalink /patch/14433/
State Accepted
Headers show

Comments

Yuya Nishihara - April 8, 2016, 3:57 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1459650050 -32400
#      Sun Apr 03 11:20:50 2016 +0900
# Node ID 170ea5eb48ccdd68010c1a03d57702728f5d53cf
# Parent  c5565fc8848dd084d104ca40c33d1acdfcff8bc6
templater: give better error message for invalid engine type

Before, KeyError was caught at changeset_templater._show(), which said "no
key named '%s'" as it was intended to catch the KeyError of unknown map key.
Instead, we should catch KeyError explicitly for better error indication.

For those who don't know what the template engine is (read "everyone"), it is
hidden extension feature that allows switching template syntax in map file.
See d8c5a7f25a40 for details.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -1011,8 +1011,11 @@  class templater(object):
     def __call__(self, t, **mapping):
         ttype = t in self.map and self.map[t][0] or 'default'
         if ttype not in self.ecache:
-            self.ecache[ttype] = engines[ttype](self.load,
-                                                 self.filters, self.defaults)
+            try:
+                ecls = engines[ttype]
+            except KeyError:
+                raise error.Abort(_('invalid template engine: %s') % ttype)
+            self.ecache[ttype] = ecls(self.load, self.filters, self.defaults)
         proc = self.ecache[ttype]
 
         stream = proc.process(t, mapping)
diff --git a/tests/test-template-engine.t b/tests/test-template-engine.t
--- a/tests/test-template-engine.t
+++ b/tests/test-template-engine.t
@@ -44,4 +44,11 @@ 
   0 97e5f848f0936960273bbf75be6388cd0350a32b -1 0000000000000000000000000000000000000000
   -1 0000000000000000000000000000000000000000 -1 0000000000000000000000000000000000000000
 
+invalid engine type:
+
+  $ echo 'changeset = unknown:changeset.txt' > unknownenginemap
+  $ hg log --style=./unknownenginemap
+  abort: invalid template engine: unknown
+  [255]
+
   $ cd ..