diff --git a/azure_functions_worker/logging.py b/azure_functions_worker/logging.py index 2f701d4bd..99b3f83b3 100644 --- a/azure_functions_worker/logging.py +++ b/azure_functions_worker/logging.py @@ -21,10 +21,17 @@ error_handler: Optional[logging.Handler] = None -def format_exception(exception): +def format_exception(exception: Exception) -> str: msg = str(exception) + "\n" - msg += ''.join(traceback.format_exception( - etype=type(exception), value=exception, tb=exception.__traceback__)) + if sys.version_info.minor < 10: + msg += ''.join(traceback.format_exception( + etype=type(exception), + tb=exception.__traceback__, + value=exception)) + elif sys.version_info.minor == 10: + msg += ''.join(traceback.format_exception(exception)) + else: + msg = str(exception) return msg diff --git a/azure_functions_worker/utils/common.py b/azure_functions_worker/utils/common.py index 350f858f2..f508a78ff 100644 --- a/azure_functions_worker/utils/common.py +++ b/azure_functions_worker/utils/common.py @@ -11,14 +11,14 @@ def is_true_like(setting: str) -> bool: if setting is None: return False - return setting.lower().strip() in ['1', 'true', 't', 'yes', 'y'] + return setting.lower().strip() in {'1', 'true', 't', 'yes', 'y'} def is_false_like(setting: str) -> bool: if setting is None: return False - return setting.lower().strip() in ['0', 'false', 'f', 'no', 'n'] + return setting.lower().strip() in {'0', 'false', 'f', 'no', 'n'} def is_envvar_true(env_key: str) -> bool: diff --git a/tests/unittests/test_logging.py b/tests/unittests/test_logging.py index 3d3465c63..1a6852331 100644 --- a/tests/unittests/test_logging.py +++ b/tests/unittests/test_logging.py @@ -3,6 +3,7 @@ import unittest from azure_functions_worker import logging as flog +from azure_functions_worker.logging import format_exception class TestLogging(unittest.TestCase): @@ -33,3 +34,27 @@ def test_customer_log_namespace(self): self.assertFalse(flog.is_system_log_category('protobuf')) self.assertFalse(flog.is_system_log_category('root')) self.assertFalse(flog.is_system_log_category('')) + + def test_format_exception(self): + def call0(fn): + call1(fn) + + def call1(fn): + call2(fn) + + def call2(fn): + fn() + + def raising_function(): + raise ValueError("Value error being raised.", ) + + try: + call0(raising_function) + except ValueError as e: + processed_exception = format_exception(e) + self.assertIn("call0", processed_exception) + self.assertIn("call1", processed_exception) + self.assertIn("call2", processed_exception) + self.assertIn("f", processed_exception) + self.assertIn("tests/unittests/test_logging.py", + processed_exception)