[PATCH] dispatch: factor out command failure handling into a function

Martijn Pieters mj at zopatista.com
Wed Mar 23 19:40:33 EDT 2016


# HG changeset patch
# User Martijn Pieters <mjpieters at fb.com>
# Date 1458775506 25200
#      Wed Mar 23 16:25:06 2016 -0700
# Node ID d45ba07deb133f30c7188d7e6f671535558c8f83
# Parent  35c2f607a1ebc57bac43e0c658168d2a7b631f87
dispatch: factor out command failure handling into a function

Moving the warning generation and re-raise to a function allows for wrapping
and alternative error handling.

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -332,60 +332,7 @@
     except socket.error as inst:
         ui.warn(_("abort: %s\n") % inst.args[-1])
     except: # re-raises
-        # For compatibility checking, we discard the portion of the hg
-        # version after the + on the assumption that if a "normal
-        # user" is running a build with a + in it the packager
-        # probably built from fairly close to a tag and anyone with a
-        # 'make local' copy of hg (where the version number can be out
-        # of date) will be clueful enough to notice the implausible
-        # version number and try updating.
-        ct = util.versiontuple(n=2)
-        worst = None, ct, ''
-        if ui.config('ui', 'supportcontact', None) is None:
-            for name, mod in extensions.extensions():
-                testedwith = getattr(mod, 'testedwith', '')
-                report = getattr(mod, 'buglink', _('the extension author.'))
-                if not testedwith.strip():
-                    # We found an untested extension. It's likely the culprit.
-                    worst = name, 'unknown', report
-                    break
-
-                # Never blame on extensions bundled with Mercurial.
-                if testedwith == 'internal':
-                    continue
-
-                tested = [util.versiontuple(t, 2) for t in testedwith.split()]
-                if ct in tested:
-                    continue
-
-                lower = [t for t in tested if t < ct]
-                nearest = max(lower or tested)
-                if worst[0] is None or nearest < worst[1]:
-                    worst = name, nearest, report
-        if worst[0] is not None:
-            name, testedwith, report = worst
-            if not isinstance(testedwith, str):
-                testedwith = '.'.join([str(c) for c in testedwith])
-            warning = (_('** Unknown exception encountered with '
-                         'possibly-broken third-party extension %s\n'
-                         '** which supports versions %s of Mercurial.\n'
-                         '** Please disable %s and try your action again.\n'
-                         '** If that fixes the bug please report it to %s\n')
-                       % (name, testedwith, name, report))
-        else:
-            bugtracker = ui.config('ui', 'supportcontact', None)
-            if bugtracker is None:
-                bugtracker = _("https://mercurial-scm.org/wiki/BugTracker")
-            warning = (_("** unknown exception encountered, "
-                         "please report by visiting\n** ") + bugtracker + '\n')
-        warning += ((_("** Python %s\n") % sys.version.replace('\n', '')) +
-                    (_("** Mercurial Distributed SCM (version %s)\n") %
-                     util.version()) +
-                    (_("** Extensions loaded: %s\n") %
-                     ", ".join([x[0] for x in extensions.extensions()])))
-        ui.log("commandexception", "%s\n%s\n", warning, traceback.format_exc())
-        ui.warn(warning)
-        raise
+        handlecommandexception(ui)
 
     return -1
 
@@ -1054,3 +1001,60 @@
                 fp.close()
     else:
         return checkargs()
+
+def handlecommandexception(ui):
+    """Produce a warning message broken commands"""
+    # For compatibility checking, we discard the portion of the hg
+    # version after the + on the assumption that if a "normal
+    # user" is running a build with a + in it the packager
+    # probably built from fairly close to a tag and anyone with a
+    # 'make local' copy of hg (where the version number can be out
+    # of date) will be clueful enough to notice the implausible
+    # version number and try updating.
+    ct = util.versiontuple(n=2)
+    worst = None, ct, ''
+    if ui.config('ui', 'supportcontact', None) is None:
+        for name, mod in extensions.extensions():
+            testedwith = getattr(mod, 'testedwith', '')
+            report = getattr(mod, 'buglink', _('the extension author.'))
+            if not testedwith.strip():
+                # We found an untested extension. It's likely the culprit.
+                worst = name, 'unknown', report
+                break
+
+            # Never blame on extensions bundled with Mercurial.
+            if testedwith == 'internal':
+                continue
+
+            tested = [util.versiontuple(t, 2) for t in testedwith.split()]
+            if ct in tested:
+                continue
+
+            lower = [t for t in tested if t < ct]
+            nearest = max(lower or tested)
+            if worst[0] is None or nearest < worst[1]:
+                worst = name, nearest, report
+    if worst[0] is not None:
+        name, testedwith, report = worst
+        if not isinstance(testedwith, str):
+            testedwith = '.'.join([str(c) for c in testedwith])
+        warning = (_('** Unknown exception encountered with '
+                     'possibly-broken third-party extension %s\n'
+                     '** which supports versions %s of Mercurial.\n'
+                     '** Please disable %s and try your action again.\n'
+                     '** If that fixes the bug please report it to %s\n')
+                   % (name, testedwith, name, report))
+    else:
+        bugtracker = ui.config('ui', 'supportcontact', None)
+        if bugtracker is None:
+            bugtracker = _("https://mercurial-scm.org/wiki/BugTracker")
+        warning = (_("** unknown exception encountered, "
+                     "please report by visiting\n** ") + bugtracker + '\n')
+    warning += ((_("** Python %s\n") % sys.version.replace('\n', '')) +
+                (_("** Mercurial Distributed SCM (version %s)\n") %
+                 util.version()) +
+                (_("** Extensions loaded: %s\n") %
+                 ", ".join([x[0] for x in extensions.extensions()])))
+    ui.log("commandexception", "%s\n%s\n", warning, traceback.format_exc())
+    ui.warn(warning)
+    raise


More information about the Mercurial-devel mailing list