Adjust exception formatting in TryAndPrint()
This commit is contained in:
parent
e83bcb864c
commit
a5b0758d30
1 changed files with 12 additions and 16 deletions
|
|
@ -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:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue