Bug 1393503 - Proper error handling for failure to find VCS tool; r?glandium draft
authorGregory Szorc <gps@mozilla.com>
Thu, 24 Aug 2017 17:09:09 -0700
changeset 652554 cb09589e9d58498e0f80164a32d3921e67702dcb
parent 651752 4f756b36b0d09bb08c1cc0381f9bad948d170439
child 728119 4160fddb3b8f6f837cc8b5d1b50689e21313cf51
push id76090
push usergszorc@mozilla.com
push dateFri, 25 Aug 2017 00:09:27 +0000
reviewersglandium
bugs1393503
milestone57.0a1
Bug 1393503 - Proper error handling for failure to find VCS tool; r?glandium ``print()`` has no business being in library code like this. It was a holdover from this code being copied from bootstrap. So remove it. While we're here, replace the generic exception with a specific one. We don't want to be swallowing bugs via ``except Exception``. MozReview-Commit-ID: 49goUstfPBz
build/mach_bootstrap.py
python/mozversioncontrol/mozversioncontrol/__init__.py
--- a/build/mach_bootstrap.py
+++ b/build/mach_bootstrap.py
@@ -164,21 +164,18 @@ def bootstrap(topsrcdir, mozilla_dir=Non
     def resolve_repository():
         import mozversioncontrol
 
         try:
             # This API doesn't respect the vcs binary choices from configure.
             # If we ever need to use the VCS binary here, consider something
             # more robust.
             return mozversioncontrol.get_repository_object(path=mozilla_dir)
-        except mozversioncontrol.InvalidRepoPath:
-            return None
-        # This is mainly to catch failures resolving the VCS binary path.
-        # TODO Change mozversioncontrol to raise non-generic exception.
-        except Exception:
+        except (mozversioncontrol.InvalidRepoPath,
+                mozversioncontrol.MissingVCSTool):
             return None
 
     def telemetry_handler(context, data):
         # We have not opted-in to telemetry
         if 'BUILD_SYSTEM_TELEMETRY' not in os.environ:
             return
 
         telemetry_dir = os.path.join(get_state_dir()[0], 'telemetry')
--- a/python/mozversioncontrol/mozversioncontrol/__init__.py
+++ b/python/mozversioncontrol/mozversioncontrol/__init__.py
@@ -9,37 +9,41 @@ import errno
 import os
 import re
 import subprocess
 import which
 
 from distutils.version import LooseVersion
 
 
+class MissingVCSTool(Exception):
+    """Represents a failure to find a version control tool binary."""
+
+
 def get_tool_path(tool):
     """Obtain the path of `tool`."""
     if os.path.isabs(tool) and os.path.exists(tool):
         return tool
 
     # We use subprocess in places, which expects a Win32 executable or
     # batch script. On some versions of MozillaBuild, we have "hg.exe",
     # "hg.bat," and "hg" (a Python script). "which" will happily return the
     # Python script, which will cause subprocess to choke. Explicitly favor
     # the Windows version over the plain script.
     try:
         return which.which(tool + '.exe')
     except which.WhichError:
         try:
             return which.which(tool)
         except which.WhichError as e:
-            print(e)
+            pass
 
-    raise Exception('Unable to obtain %s path. Try running '
-                    '|mach bootstrap| to ensure your environment is up to '
-                    'date.' % tool)
+    raise MissingVCSTool('Unable to obtain %s path. Try running '
+                         '|mach bootstrap| to ensure your environment is up to '
+                         'date.' % tool)
 
 
 class Repository(object):
     __metaclass__ = abc.ABCMeta
 
     '''A class wrapping utility methods around version control repositories.'''
     def __init__(self, path, tool):
         self.path = os.path.abspath(path)