From 37f14a82c7c382027b884dace296c50f74842b37 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 11 Mar 2024 14:41:23 -0700 Subject: [PATCH] actions: Minor refactor to action error logging For reuse with HTML formatting. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/actions.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/plinth/actions.py b/plinth/actions.py index 8f9f1e802..eb5908d5e 100644 --- a/plinth/actions.py +++ b/plinth/actions.py @@ -132,11 +132,11 @@ def _wait_for_return(module_name, action_name, args, kwargs, log_error, proc, """Communicate with the subprocess and wait for its return.""" json_args = json.dumps({'args': args, 'kwargs': kwargs}) - output, error = proc.communicate(input=json_args.encode()) + stdout, stderr = proc.communicate(input=json_args.encode()) read_thread.join() if proc.returncode != 0: - logger.error('Error executing command - %s, %s, %s', command, output, - error) + logger.error('Error executing command - %s, %s, %s', command, stdout, + stderr) raise subprocess.CalledProcessError(proc.returncode, command) try: @@ -151,32 +151,36 @@ def _wait_for_return(module_name, action_name, args, kwargs, log_error, proc, module = importlib.import_module(return_value['exception']['module']) exception_class = getattr(module, return_value['exception']['name']) - exception = exception_class(*return_value['exception']['args'], output, - error) + exception = exception_class(*return_value['exception']['args']) + exception.stdout = stdout + exception.stderr = stderr + if log_error: - _log_error(module_name, action_name, args, kwargs, exception, - return_value) + full_args, exception_args, stdout, stderr, traceback = _format_error( + args, kwargs, exception, return_value) + logger.error('Error running action %s..%s(%s): %s(%s)\n' + '%s%s%s', module_name, action_name, full_args, + return_value['exception']['name'], exception_args, stdout, + stderr, traceback) raise exception -def _log_error(module_name, action_name, args, kwargs, exception, - return_value): +def _format_error(args, kwargs, exception, return_value): """Log the exception in a readable manner.""" args = [json.dumps(arg) for arg in args] kwargs = [f'{key}=' + json.dumps(value) for key, value in kwargs.items()] full_args = ', '.join(args + kwargs) - exception_args = ', '.join( - [json.dumps(arg) for arg in exception.args[:-2]]) + exception_args = ', '.join([json.dumps(arg) for arg in exception.args]) - stdout = exception.args[-2].decode() + stdout = exception.stdout.decode() if stdout: lines = stdout.split('\n') lines = lines[:-1] if not lines[-1] else lines stdout = '\n'.join(('│ ' + line for line in lines)) stdout = 'Stdout:\n' + stdout + '\n' - stderr = exception.args[-1].decode() + stderr = exception.stderr.decode() if stderr: lines = stderr.split('\n') lines = lines[:-1] if not lines[-1] else lines @@ -193,10 +197,7 @@ def _log_error(module_name, action_name, args, kwargs, exception, traceback = '\n'.join(('╞ ' + line for line in all_lines)) traceback = 'Action traceback:\n' + traceback + '\n' - logger.error('Error running action %s..%s(%s): %s(%s)\n' - '%s%s%s', module_name, action_name, full_args, - exception.__class__.__name__, exception_args, stdout, stderr, - traceback) + return (full_args, exception_args, stdout, stderr, traceback) def _thread_reader(read_fd, buffers):