From 7c08990a45540a83695e7759af0c72b77a90c2d5 Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Thu, 14 Aug 2025 10:33:00 +1200 Subject: [PATCH] samba-tool: add verbose flag to @exception_to_command_error Helpful in development. Signed-off-by: Douglas Bagnall Reviewed-by: Gary Lockyer --- python/samba/netcmd/__init__.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 -- 2.47.3