diff --git a/python/packages/core/agent_framework/exceptions.py b/python/packages/core/agent_framework/exceptions.py index 4f56c34b5c..d5a54c2102 100644 --- a/python/packages/core/agent_framework/exceptions.py +++ b/python/packages/core/agent_framework/exceptions.py @@ -34,7 +34,8 @@ def __init__( logger.log(log_level, message, exc_info=inner_exception) if inner_exception: super().__init__(message, inner_exception, *args) # type: ignore - super().__init__(message, *args) # type: ignore + else: + super().__init__(message, *args) # type: ignore # region Agent Exceptions diff --git a/python/packages/core/tests/core/test_exceptions.py b/python/packages/core/tests/core/test_exceptions.py new file mode 100644 index 0000000000..47b3a53197 --- /dev/null +++ b/python/packages/core/tests/core/test_exceptions.py @@ -0,0 +1,29 @@ +# Copyright (c) Microsoft. All rights reserved. + +"""Tests for AgentFrameworkException inner_exception handling.""" + +import pytest + +from agent_framework import AgentFrameworkException + + +def test_exception_with_inner_exception(): + """When inner_exception is provided, it should be set as the second arg.""" + inner = ValueError("inner error") + exc = AgentFrameworkException("test message", inner_exception=inner) + assert exc.args[0] == "test message" + assert exc.args[1] is inner + + +def test_exception_without_inner_exception(): + """When inner_exception is None, args should only contain the message.""" + exc = AgentFrameworkException("test message") + assert exc.args == ("test message",) + assert len(exc.args) == 1 + + +def test_exception_inner_exception_none_explicit(): + """When inner_exception is explicitly None, args should only contain the message.""" + exc = AgentFrameworkException("test message", inner_exception=None) + assert exc.args == ("test message",) + assert len(exc.args) == 1