]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba-tool: add verbose flag to @exception_to_command_error
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 13 Aug 2025 22:33:00 +0000 (10:33 +1200)
committerDouglas Bagnall <dbagnall@samba.org>
Wed, 20 Aug 2025 04:34:37 +0000 (04:34 +0000)
Helpful in development.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
python/samba/netcmd/__init__.py

index 3ab683ca66441b589012f56d9e4d5bc2315b0823..0647ed621d6085e8711fabe52d74e381f19248c5 100644 (file)
@@ -502,19 +502,37 @@ class CommandError(Exception):
         return "CommandError(%s)" % self.message
 
 
-def exception_to_command_error(*exceptions):
+def exception_to_command_error(*exceptions, verbose=False):
     """If you think all instances of a particular exceptions can be
     turning to a CommandError, do this:
 
     @exception_to_command_error(ValueError, LdbError):
     def run(self, username, ...):
         # continue as normal
+
+    Add the verbose=True flag during development if it is doing your
+    head in.
     """
     def wrap2(f):
         def wrap(*args, **kwargs):
             try:
                 return f(*args, **kwargs)
             except exceptions as e:
+                if verbose:
+                    print(colour.c_DARK_RED("↓" * 20 + "DEBUG" + "↓" * 20),
+                          file=sys.stderr)
+                    print(f"converting «e» raised in {f} "
+                          "to CommandError\n",
+                          file=sys.stderr)
+                    traceback.print_exc()
+                    print("\nThis message is here because "
+                          "exception_to_command_error() has the verbose=True"
+                          " debugging option set.",
+                          file=sys.stderr)
+                    print("Below this is what you would normally see:",
+                          file=sys.stderr)
+                    print(colour.c_DARK_RED("↑" * 20 + "DEBUG" + "↑" * 20),
+                          file=sys.stderr)
                 raise CommandError(e)
         return wrap
     return wrap2