Patchwork D10737: errors: make InterventionRequired subclass Abort

login
register
mail settings
Submitter phabricator
Date May 19, 2021, 5:48 a.m.
Message ID <differential-rev-PHID-DREV-zlpeixiy5zpxbhhk2uj7-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49058/
State Superseded
Headers show

Comments

phabricator - May 19, 2021, 5:48 a.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The docstring for `Abort` says that it's for errors raised by commands
  and `InterventionRequired` is definitely something raised by commands,
  so it seems that it should be an `Abort`. This patch makes it so. It
  adds a `coarse_exit_code` (in addition to the already existing
  `detailed_exit_code`) to `Abort` to achieve that, since
  `InterventionRequired` should result in a special exit code even when
  the `ui.detailed-exit-code` config is not set.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10737

AFFECTED FILES
  mercurial/error.py
  mercurial/scmutil.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -203,17 +203,13 @@ 
         if inst.hint:
             ui.error(_(b"(%s)\n") % inst.hint)
         detailed_exit_code = 50
-    except error.InterventionRequired as inst:
-        ui.error(b"%s\n" % inst)
-        if inst.hint:
-            ui.error(_(b"(%s)\n") % inst.hint)
-        detailed_exit_code = 240
-        coarse_exit_code = 1
     except error.WdirUnsupported:
         ui.error(_(b"abort: working directory revision cannot be specified\n"))
     except error.Abort as inst:
         if inst.detailed_exit_code is not None:
             detailed_exit_code = inst.detailed_exit_code
+        if inst.coarse_exit_code is not None:
+            coarse_exit_code = inst.coarse_exit_code
         ui.error(inst.format())
     except error.WorkerError as inst:
         # Don't print a message -- the worker already should have
diff --git a/mercurial/error.py b/mercurial/error.py
--- a/mercurial/error.py
+++ b/mercurial/error.py
@@ -54,10 +54,13 @@ 
 class Abort(Hint, Exception):
     """Raised if a command needs to print an error and exit."""
 
-    def __init__(self, message, hint=None, detailed_exit_code=None):
+    def __init__(
+        self, message, hint=None, coarse_exit_code=None, detailed_exit_code=None
+    ):
         # type: (bytes, Optional[bytes]) -> None
         self.message = message
         self.hint = hint
+        self.coarse_exit_code = coarse_exit_code
         self.detailed_exit_code = detailed_exit_code
         # Pass the message into the Exception constructor to help extensions
         # that look for exc.args[0].
@@ -192,10 +195,22 @@ 
     __bytes__ = _tobytes
 
 
-class InterventionRequired(Hint, Exception):
+class InterventionRequired(Abort):
     """Exception raised when a command requires human intervention."""
 
-    __bytes__ = _tobytes
+    def __init__(self, message, hint=None):
+        super(InterventionRequired, self).__init__(
+            message, hint=hint, coarse_exit_code=1, detailed_exit_code=240
+        )
+
+    def format(self):
+        # type: () -> bytes
+        from .i18n import _
+
+        message = _(b"%s\n") % self.message
+        if self.hint:
+            message += _(b"(%s)\n") % self.hint
+        return message
 
 
 class ConflictResolutionRequired(InterventionRequired):