From: Douglas Bagnall Date: Wed, 13 Aug 2025 22:33:00 +0000 (+1200) Subject: samba-tool: add verbose flag to @exception_to_command_error X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7c08990a45540a83695e7759af0c72b77a90c2d5;p=thirdparty%2Fsamba.git samba-tool: add verbose flag to @exception_to_command_error Helpful in development. Signed-off-by: Douglas Bagnall Reviewed-by: Gary Lockyer --- diff --git a/python/samba/netcmd/__init__.py b/python/samba/netcmd/__init__.py index 3ab683ca664..0647ed621d6 100644 --- a/python/samba/netcmd/__init__.py +++ b/python/samba/netcmd/__init__.py @@ -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