|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import unittest |
| 5 | +import traceback |
| 6 | +from azure_functions_runtime_v1.utils.tracing import (extend_exception_message, |
| 7 | + marshall_exception_trace, |
| 8 | + serialize_exception, |
| 9 | + serialize_exception_as_str) |
| 10 | + |
| 11 | + |
| 12 | +class MockProtos: |
| 13 | + class RpcException: |
| 14 | + def __init__(self, message, stack_trace): |
| 15 | + self.message = message |
| 16 | + self.stack_trace = stack_trace |
| 17 | + |
| 18 | + |
| 19 | +class TestExceptionUtils(unittest.TestCase): |
| 20 | + |
| 21 | + def test_extend_exception_message_basic(self): |
| 22 | + exc = ValueError("Original message") |
| 23 | + new_msg = "Extra info" |
| 24 | + new_exc = extend_exception_message(exc, new_msg) |
| 25 | + self.assertIsInstance(new_exc, ValueError) |
| 26 | + self.assertIn("Original message", str(new_exc)) |
| 27 | + self.assertIn("Extra info", str(new_exc)) |
| 28 | + self.assertTrue(str(new_exc).endswith(new_msg)) |
| 29 | + |
| 30 | + def test_extend_exception_message_no_dot(self): |
| 31 | + exc = ValueError("Message without dot") |
| 32 | + new_exc = extend_exception_message(exc, "added") |
| 33 | + self.assertEqual(str(new_exc), "Message without dot. added") |
| 34 | + |
| 35 | + def test_marshall_exception_trace_basic(self): |
| 36 | + try: |
| 37 | + raise ValueError("Test") |
| 38 | + except ValueError as exc: |
| 39 | + trace = marshall_exception_trace(exc) |
| 40 | + self.assertIn("ValueError: Test", trace) |
| 41 | + self.assertIn("raise ValueError", trace) |
| 42 | + |
| 43 | + def test_marshall_exception_trace_module_not_found(self): |
| 44 | + try: |
| 45 | + import non_existent_module # noqa: F401 |
| 46 | + except ModuleNotFoundError as exc: |
| 47 | + trace = marshall_exception_trace(exc) |
| 48 | + self.assertIn("ModuleNotFoundError", trace) |
| 49 | + self.assertNotIn("<frozen importlib._bootstrap>", trace) |
| 50 | + |
| 51 | + def test_marshall_exception_trace_chained_exceptions(self): |
| 52 | + try: |
| 53 | + try: |
| 54 | + raise ValueError("Inner error") |
| 55 | + except ValueError as inner: |
| 56 | + raise RuntimeError("Outer error") from inner |
| 57 | + except RuntimeError as exc: |
| 58 | + trace = marshall_exception_trace(exc) |
| 59 | + # Outer exception must appear |
| 60 | + self.assertIn("RuntimeError: Outer error", trace) |
| 61 | + # Inner exception must also appear |
| 62 | + self.assertIn("ValueError: Inner error", trace) |
| 63 | + # Ensure 'The above exception was the direct cause' appears |
| 64 | + self.assertIn("The above exception was the direct cause", trace) |
| 65 | + |
| 66 | + def test_serialize_exception_returns_rpc_exception(self): |
| 67 | + try: |
| 68 | + raise ValueError("Error for proto") |
| 69 | + except ValueError as exc: |
| 70 | + result = serialize_exception(exc, MockProtos) |
| 71 | + self.assertIsInstance(result, MockProtos.RpcException) |
| 72 | + self.assertIn("ValueError", result.message) |
| 73 | + self.assertIn("Error for proto", result.message) |
| 74 | + self.assertIn("raise ValueError", result.stack_trace) |
| 75 | + |
| 76 | + def test_serialize_exception_as_str_basic(self): |
| 77 | + try: |
| 78 | + raise RuntimeError("Runtime issue") |
| 79 | + except RuntimeError as exc: |
| 80 | + result = serialize_exception_as_str(exc) |
| 81 | + self.assertIn("RuntimeError: Runtime issue", result) |
| 82 | + self.assertIn("Stack Trace:", result) |
| 83 | + self.assertIn("raise RuntimeError", result) |
| 84 | + |
| 85 | + def test_serialize_exception_with_unserializable_exception(self): |
| 86 | + class BadExc(Exception): |
| 87 | + def __str__(self): |
| 88 | + raise ValueError("Cannot stringify") |
| 89 | + |
| 90 | + exc = BadExc() |
| 91 | + result_str = serialize_exception_as_str(exc) |
| 92 | + self.assertIn("Could not serialize original exception message", result_str) |
| 93 | + |
| 94 | + result_proto = serialize_exception(exc, MockProtos) |
| 95 | + self.assertIn("Could not serialize original exception message", |
| 96 | + result_proto.message) |
| 97 | + |
| 98 | + def test_marshall_exception_trace_sub_exception(self): |
| 99 | + # Patch traceback.format_exception to raise inside marshall_exception_trace |
| 100 | + original_format_exception = traceback.format_exception |
| 101 | + |
| 102 | + def bad_format(*args, **kwargs): |
| 103 | + raise RuntimeError("fail inside traceback") |
| 104 | + traceback.format_exception = bad_format |
| 105 | + try: |
| 106 | + exc = ValueError("test") |
| 107 | + result = marshall_exception_trace(exc) |
| 108 | + self.assertIn("Could not extract traceback", result) |
| 109 | + self.assertIn("RuntimeError", result) |
| 110 | + finally: |
| 111 | + traceback.format_exception = original_format_exception |
0 commit comments