STL API: change STLError representation 90/5390/1
authorYaroslav Brustinov <[email protected]>
Tue, 7 Feb 2017 19:10:20 +0000 (21:10 +0200)
committerYaroslav Brustinov <[email protected]>
Tue, 7 Feb 2017 19:10:20 +0000 (21:10 +0200)
Change-Id: I1d4f4f5b11764f720713811c019802eeac3f5071
Signed-off-by: Yaroslav Brustinov <[email protected]>
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_exceptions.py

index 79a001f..f753364 100644 (file)
@@ -13,19 +13,22 @@ except NameError:
 class STLError(Exception):
     def __init__ (self, msg):
         self.msg = str(msg)
-        self.tb = traceback.extract_stack()
+        self.stack = traceback.extract_stack()
 
     def __str__ (self):
+        self.tb = traceback.extract_tb(sys.exc_info()[2])
+        if not self.tb:
+            return self.msg
 
         s = format_text("\n******\n", 'bold')
-        s += format_text('\nSummary error report:\n\n', 'underline')
-        s += format_text(self.msg + '\n', 'bold')
-        
-        s += format_text("\nFull error report:\n\n", 'underline')
-        
-        for line in reversed(self.tb[:-1]):
+        s += format_text("\nException stack (most recent call last):\n\n", 'underline')
+
+        for i, line in enumerate(self.tb):
             fname, lineno, func, src = os.path.split(line[0])[1], line[1], line[2], line[3]
-            s += "         {:<50} - '{}'\n".format(format_text(fname, 'bold') + ':' + format_text(lineno, 'bold'), format_text(src.strip(), 'bold'))
+            s += "#{:<2}    {:<50} - '{}'\n".format(len(self.tb) - i - 1, format_text(fname, 'bold') + ':' + format_text(lineno, 'bold'), format_text(src.strip(), 'bold'))
+
+        s += format_text('\nSummary error message:\n\n', 'underline')
+        s += format_text(self.msg + '\n', 'bold')
 
         return s