From afbaca96f0c6e35e3fec6f0cce0c037f41addbb6 Mon Sep 17 00:00:00 2001 From: Nageswara Nandigam Date: Fri, 9 Sep 2022 12:41:58 -0700 Subject: [PATCH 1/2] update format_exception etype argument --- azurelinuxagent/common/utils/textutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurelinuxagent/common/utils/textutil.py b/azurelinuxagent/common/utils/textutil.py index 153eb80643..1ff7a7e912 100644 --- a/azurelinuxagent/common/utils/textutil.py +++ b/azurelinuxagent/common/utils/textutil.py @@ -445,7 +445,7 @@ def format_exception(exception): if tb is None or (sys.version_info[0] == 2 and e != exception): msg += "[Traceback not available]" else: - msg += ''.join(traceback.format_exception(etype=type(exception), value=exception, tb=tb)) + msg += ''.join(traceback.format_exception(type(exception), value=exception, tb=tb)) return msg From 02455742ba1bdac68cd856fc76181bef6a7f613a Mon Sep 17 00:00:00 2001 From: Nageswara Nandigam Date: Fri, 9 Sep 2022 17:14:56 -0700 Subject: [PATCH 2/2] unit test --- tests/utils/test_text_util.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/utils/test_text_util.py b/tests/utils/test_text_util.py index 3b29f93694..ff129c40be 100644 --- a/tests/utils/test_text_util.py +++ b/tests/utils/test_text_util.py @@ -206,6 +206,26 @@ def test_format_memory_value(self): self.assertRaises(ValueError, textutil.format_memory_value, 'KiloBytes', 1) self.assertRaises(TypeError, textutil.format_memory_value, 'bytes', None) + def test_format_exception(self): + """ + Test formatting of exception into human-readable format + """ + + def raise_exception(count=3): + if count <= 1: + raise Exception("Test Exception") + raise_exception(count - 1) + + msg = "" + try: + raise_exception() + except Exception as e: + msg = textutil.format_exception(e) + + self.assertIn("Test Exception", msg) + # Raise exception at count 1 after two nested calls since count starts at 3 + self.assertEqual(2, msg.count("raise_exception(count - 1)")) + if __name__ == '__main__': unittest.main()