actions: Minor refactor to action error logging

For reuse with HTML formatting.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2024-03-11 14:41:23 -07:00 committed by James Valleroy
parent 5b299068a8
commit 37f14a82c7
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -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):