logger: handle chained exceptions

Fixes #1456

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2018-12-26 03:38:34 +02:00
parent 80c5ff7b64
commit feb6f48a5a
3 changed files with 88 additions and 13 deletions
+2
View File
@@ -3,6 +3,8 @@ import traceback
class DvcException(Exception):
def __init__(self, msg, cause=None):
# NOTE: unlike python 3, python 2 doesn't have built-in support
# for chained exceptions, so we are using our own implementation.
self.cause = cause
self.cause_tb = None
if cause:
+35 -13
View File
@@ -30,6 +30,8 @@ def warning(message):
def error(message=None):
prefix = colorize('Error', color='red')
exception, stack_trace = _parse_exc()
out = (
'{prefix}: {description}'
'\n'
@@ -37,8 +39,8 @@ def error(message=None):
'\n'
'{footer}'.format(
prefix=prefix,
description=_description(message),
stack_trace=_stack_trace(),
description=_description(message, exception),
stack_trace=stack_trace,
footer=_footer(),
)
)
@@ -188,21 +190,41 @@ def _add_handlers():
logger.addHandler(sh_err)
def _stack_trace():
trace = sys.exc_info()[2]
def _walk_exc(exc):
exc_list = [str(exc)]
tb_list = [traceback.format_exc()]
if not is_verbose() or not trace:
return ''
# NOTE: parsing chained exceptions. See dvc/exceptions.py for more info.
while hasattr(exc, 'cause') and exc.cause is not None:
exc_list.append(str(exc.cause))
if hasattr(exc, 'cause_tb') and exc.cause_tb is not None:
tb_list.insert(0, str(exc.cause_tb))
exc = exc.cause
return '{line}\n{stack_trace}{line}\n'.format(
line=colorize('-' * 60, color='red'),
stack_trace=traceback.format_exc(),
)
return (exc_list, tb_list)
def _description(message):
exception = sys.exc_info()[1]
def _parse_exc():
exc = sys.exc_info()[1]
if not exc:
return (None, '')
exc_list, tb_list = _walk_exc(exc)
exception = ': '.join(exc_list)
if is_verbose():
stack_trace = '{line}\n{stack_trace}{line}\n'.format(
line=colorize('-' * 60, color='red'),
stack_trace='\n'.join(tb_list),
)
else:
stack_trace = ''
return (exception, stack_trace)
def _description(message, exception):
if exception and message:
description = '{message} - {exception}'
elif exception:
@@ -210,7 +232,7 @@ def _description(message):
elif message:
description = '{message}'
return description.format(message=message, exception=str(exception))
return description.format(message=message, exception=exception)
def _footer():
+51
View File
@@ -1,3 +1,4 @@
import traceback
from mock import patch
from unittest import TestCase
@@ -9,6 +10,7 @@ except ImportError:
import logging
import dvc.logger as logger
from dvc.command.base import CmdBase
from dvc.exceptions import DvcException
class TestLogger(TestCase):
@@ -92,6 +94,55 @@ class TestLogger(TestCase):
self.assertEqual(self.stderr, output)
def test_error_with_chained_exception_and_message(self):
try:
raise DvcException('exception', cause=Exception('cause'))
except Exception:
logger.error('message')
output = (
'Error: message - exception: cause\n'
'\n'
'Having any troubles? Hit us up at https://dvc.org/support,'
' we are always happy to help!\n'
)
self.assertEqual(self.stderr, output)
def test_traceback(self):
stack_trace1 = 'stack_trace1\n'
stack_trace2 = 'stack_trace2\n'
try:
exc1 = Exception('exception1')
exc2 = DvcException('exception2', cause=exc1)
exc2.cause_tb = stack_trace1
exc3 = DvcException('exception3', cause=exc2)
exc3.cause_tb = stack_trace2
raise exc3
except Exception:
stack_trace3 = traceback.format_exc()
with logger.verbose():
logger.error('message')
output = (
'Error: message - exception3: exception2: exception1\n'
'{line}\n'
'{stack_trace1}'
'\n'
'{stack_trace2}'
'\n'
'{stack_trace3}'
'{line}\n'
'\n'
'Having any troubles? Hit us up at https://dvc.org/support,'
' we are always happy to help!\n'
).format(line='-' * 60,
stack_trace1=stack_trace1,
stack_trace2=stack_trace2,
stack_trace3=stack_trace3)
self.assertEqual(self.stderr, output)
def test_box(self):
logger.box('message')