Repository: iterative/dvc

Issue:
logger: handle chained exceptions
Current logger logic introduced in https://github.com/iterative/dvc/pull/1454 doesn't handle chained exceptions as the old logic did. Since we need to support both python3 and python2, which doesn't have built-in support for exception chaining, we use our own chaining mechanism https://github.com/iterative/dvc/blob/master/dvc/exceptions.py#L6 . See https://github.com/iterative/dvc/blob/0.23.2/dvc/logger.py#L100 .

Discussion:

Patch:
diff --git a/dvc/exceptions.py b/dvc/exceptions.py
index d3da24075..81eea2fa5 100644
--- a/dvc/exceptions.py
+++ b/dvc/exceptions.py
@@ -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:
diff --git a/dvc/logger.py b/dvc/logger.py
index 4c075f268..7624b3e74 100644
--- a/dvc/logger.py
+++ b/dvc/logger.py
@@ -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 _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 = sys.exc_info()[1]
 
+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():


Test patch:
diff --git a/tests/test_logger.py b/tests/test_logger.py
index bd619489b..4ba48548e 100644
--- a/tests/test_logger.py
+++ b/tests/test_logger.py
@@ -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')