Patchwork [1,of,2] templater: show slightly better hint on map operation error

login
register
mail settings
Submitter Yuya Nishihara
Date June 8, 2018, 2:52 p.m.
Message ID <ac754116b7cdebdcf41b.1528469549@mimosa>
Download mbox | patch
Permalink /patch/32035/
State Accepted
Headers show

Comments

Yuya Nishihara - June 8, 2018, 2:52 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1528459074 -32400
#      Fri Jun 08 20:57:54 2018 +0900
# Node ID ac754116b7cdebdcf41b8de8a9e9b2b8e6006e4e
# Parent  863c2656c97cbfaaf26e0812508e5072d21911cb
templater: show slightly better hint on map operation error

It was super hard to find where the templater went wrong if a keyword was
evaluated to '' or None.

Patch

diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -753,14 +753,22 @@  def _iteroverlaymaps(context, origmappin
         lm['index'] = i
         yield lm
 
-def _applymap(context, mapping, d, targ):
-    for lm in _iteroverlaymaps(context, mapping, d.itermaps(context)):
+def _applymap(context, mapping, d, darg, targ):
+    try:
+        diter = d.itermaps(context)
+    except error.ParseError as err:
+        sym = findsymbolicname(darg)
+        if not sym:
+            raise
+        hint = _("keyword '%s' does not support map operation") % sym
+        raise error.ParseError(bytes(err), hint=hint)
+    for lm in _iteroverlaymaps(context, mapping, diter):
         yield evalrawexp(context, lm, targ)
 
 def runmap(context, mapping, data):
     darg, targ = data
     d = evalwrapped(context, mapping, darg)
-    return mappedgenerator(_applymap, args=(mapping, d, targ))
+    return mappedgenerator(_applymap, args=(mapping, d, darg, targ))
 
 def runmember(context, mapping, data):
     darg, memb = data
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
@@ -3213,6 +3213,7 @@  Test new-style inline templating:
 
   $ hg log -R latesttag -r tip -T '{rev % "a"}\n'
   hg: parse error: 11 is not iterable of mappings
+  (keyword 'rev' does not support map operation)
   [255]
   $ hg log -R latesttag -r tip -T '{get(extras, "unknown") % "a"}\n'
   hg: parse error: None is not iterable of mappings