Bugfix updates for try_and_print()

This commit is contained in:
2Shirt 2019-08-09 21:57:41 -06:00
parent 4100c38280
commit b314a9f1e2
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -181,7 +181,10 @@ def clear_screen():
def format_exception_message(_exception, indent=INDENT, width=WIDTH): def format_exception_message(_exception, indent=INDENT, width=WIDTH):
"""Format using the exception's args or name, returns str.""" """Format using the exception's args or name, returns str."""
# pylint: disable=broad-except # pylint: disable=broad-except
LOG.debug('Formatting exception: %s', _exception) LOG.debug(
'Formatting exception: %s',
_exception.__class__.__name__,
)
message = None message = None
# Use known argument index or first string found # Use known argument index or first string found
@ -193,6 +196,8 @@ def format_exception_message(_exception, indent=INDENT, width=WIDTH):
message = message.strip() message = message.strip()
elif isinstance(_exception, FileNotFoundError): elif isinstance(_exception, FileNotFoundError):
message = _exception.args[1] message = _exception.args[1]
elif isinstance(_exception, ZeroDivisionError):
message = 'ZeroDivisionError'
else: else:
for arg in _exception.args: for arg in _exception.args:
if isinstance(arg, str): if isinstance(arg, str):
@ -229,6 +234,9 @@ def format_function_output(output, indent=INDENT, width=WIDTH):
"""Format function output for use in try_and_print(), returns str.""" """Format function output for use in try_and_print(), returns str."""
LOG.debug('Formatting output: %s', output) LOG.debug('Formatting output: %s', output)
if not output:
raise GenericWarning('No output')
# Ensure we're working with a list # Ensure we're working with a list
if isinstance(output, CompletedProcess): if isinstance(output, CompletedProcess):
stdout = output.stdout stdout = output.stdout
@ -531,6 +539,12 @@ def try_and_print(
f_exception = None f_exception = None
output = None output = None
result_msg = 'UNKNOWN' result_msg = 'UNKNOWN'
# Build tuples of exceptions
if not w_exceptions:
w_exceptions = ('GenericWarning',)
if not e_exceptions:
e_exceptions = ('GenericError',)
w_exceptions = tuple(get_exception(e) for e in w_exceptions) w_exceptions = tuple(get_exception(e) for e in w_exceptions)
e_exceptions = tuple(get_exception(e) for e in e_exceptions) e_exceptions = tuple(get_exception(e) for e in e_exceptions)
@ -541,9 +555,9 @@ def try_and_print(
output = function(*args, **kwargs) output = function(*args, **kwargs)
if print_return: if print_return:
result_msg = format_function_output(output, indent, width) result_msg = format_function_output(output, indent, width)
print(result_msg)
else: else:
result_msg = msg_good print_success(msg_good)
print_success(result_msg)
except w_exceptions as _exception: except w_exceptions as _exception:
result_msg = format_exception_message(_exception, indent, width) result_msg = format_exception_message(_exception, indent, width)
print_warning(result_msg) print_warning(result_msg)
@ -559,10 +573,9 @@ def try_and_print(
result_msg = msg_bad result_msg = msg_bad
print_error(result_msg) print_error(result_msg)
f_exception = _exception f_exception = _exception
if not catch_all:
# Re-raise error if necessary # Re-raise error as necessary
if f_exception and not catch_all: raise
raise #pylint: disable=misplaced-bare-raise
# Done # Done
return { return {