Adjust exception formatting in TryAndPrint()

This commit is contained in:
2Shirt 2021-05-13 21:08:38 -06:00
parent e83bcb864c
commit a5b0758d30
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -419,37 +419,33 @@ class TryAndPrint():
def _format_exception_message(self, _exception): def _format_exception_message(self, _exception):
"""Format using the exception's args or name, returns str.""" """Format using the exception's args or name, returns str."""
LOG.debug( LOG.debug(
'Formatting exception: %s', 'Formatting exception: %s, %s',
_exception.__class__.__name__, _exception.__class__.__name__,
_exception,
) )
message = None message = ''
# Use known argument index or first string found # Format message string from _exception
try: try:
if isinstance(_exception, subprocess.CalledProcessError): if isinstance(_exception, subprocess.CalledProcessError):
message = _exception.stderr message = _exception.stderr
if not isinstance(message, str): if not isinstance(message, str):
message = message.decode('utf-8') message = message.decode('utf-8')
message = message.strip() message = message.strip()
elif isinstance(_exception, FileNotFoundError):
message = _exception.args[1]
elif isinstance(_exception, ZeroDivisionError): elif isinstance(_exception, ZeroDivisionError):
message = 'ZeroDivisionError' # Skip and just use exception name below
pass
else: else:
for arg in _exception.args: message = str(_exception)
if isinstance(arg, str):
message = arg
break
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
# Just use the exception name instead # Just use the exception name instead
pass pass
# Safety check # Prepend exception name
if not message: try:
try: message = f'{_exception.__class__.__name__}: {message}'
message = _exception.__class__.__name__ except Exception: # pylint: disable=broad-except
except Exception: # pylint: disable=broad-except message = f'UNKNOWN ERROR: {message}'
message = 'UNKNOWN ERROR'
# Fix multi-line messages # Fix multi-line messages
if '\n' in message: if '\n' in message: