phabricator: avoid a stacktrace when command arguments are missing
authorMatt Harbison <matt_harbison@yahoo.com>
Wed, 04 Mar 2020 00:45:54 -0500
changeset 44436 09f3e003fc2a
parent 44435 4152183acedd
child 44437 6a34e438461b
phabricator: avoid a stacktrace when command arguments are missing Previously, the TypeError wasn't properly converted to a SignatureError when improper arguments were supplied to the inner function, because the stack depth is 2 inside the vcrcommand decorator. The `__name__` and `__doc__` attributes need to be reassigned to the new wrapper so that the help summary is available. Differential Revision: https://phab.mercurial-scm.org/D8209
hgext/phabricator.py
mercurial/util.py
tests/test-phabricator.t
--- a/hgext/phabricator.py	Fri Jan 24 15:51:19 2020 -0800
+++ b/hgext/phabricator.py	Wed Mar 04 00:45:54 2020 -0500
@@ -257,15 +257,17 @@
                         return fn(*args, **kwargs)
             return fn(*args, **kwargs)
 
-        inner.__name__ = fn.__name__
-        inner.__doc__ = fn.__doc__
+        cmd = util.checksignature(inner, depth=2)
+        cmd.__name__ = fn.__name__
+        cmd.__doc__ = fn.__doc__
+
         return command(
             name,
             fullflags,
             spec,
             helpcategory=helpcategory,
             optionalrepo=optionalrepo,
-        )(inner)
+        )(cmd)
 
     return decorate
 
--- a/mercurial/util.py	Fri Jan 24 15:51:19 2020 -0800
+++ b/mercurial/util.py	Wed Mar 04 00:45:54 2020 -0500
@@ -1846,14 +1846,14 @@
     return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.'
 
 
-def checksignature(func):
+def checksignature(func, depth=1):
     '''wrap a function with code to check for calling errors'''
 
     def check(*args, **kwargs):
         try:
             return func(*args, **kwargs)
         except TypeError:
-            if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
+            if len(traceback.extract_tb(sys.exc_info()[2])) == depth:
                 raise error.SignatureError
             raise
 
--- a/tests/test-phabricator.t	Fri Jan 24 15:51:19 2020 -0800
+++ b/tests/test-phabricator.t	Wed Mar 04 00:45:54 2020 -0500
@@ -29,6 +29,21 @@
   >  --test-vcr "$VCR/phabread-conduit-error.json" D4480 | head
   abort: Conduit Error (ERR-INVALID-AUTH): API token "cli-notavalidtoken" has the wrong length. API tokens should be 32 characters long.
 
+Missing arguments print the command help
+
+  $ hg phabread
+  hg phabread: invalid arguments
+  hg phabread DREVSPEC [OPTIONS]
+  
+  print patches from Phabricator suitable for importing
+  
+  options:
+  
+    --stack read dependencies
+  
+  (use 'hg phabread -h' to show more help)
+  [255]
+
 Basic phabread:
   $ hg phabread --test-vcr "$VCR/phabread-4480.json" D4480 | head
   # HG changeset patch