Skip to content

Commit c9d0493

Browse files
committed
feat: Flip exception_to_structured_error default to raise_error=True (#518)
Now that all tools are migrated to use ToolError, flip the default behavior of exception_to_structured_error to raise by default. This completes the ToolError migration - all error paths now signal errors at the MCP protocol level with isError=true. https://claude.ai/code/session_01MvDDV6qmWosBfGhmTLYzo8
1 parent 1b8f7d7 commit c9d0493

2 files changed

Lines changed: 13 additions & 24 deletions

File tree

src/ha_mcp/tools/helpers.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,46 +88,43 @@ def exception_to_structured_error(
8888
error: Exception,
8989
context: dict[str, Any] | None = None,
9090
*,
91-
raise_error: Literal[False] = False,
92-
) -> dict[str, Any]: ...
91+
raise_error: Literal[True] = True,
92+
) -> NoReturn: ...
9393

9494

9595
@overload
9696
def exception_to_structured_error(
9797
error: Exception,
9898
context: dict[str, Any] | None = None,
9999
*,
100-
raise_error: Literal[True],
101-
) -> NoReturn: ...
100+
raise_error: Literal[False],
101+
) -> dict[str, Any]: ...
102102

103103

104104
def exception_to_structured_error(
105105
error: Exception,
106106
context: dict[str, Any] | None = None,
107107
*,
108-
raise_error: bool = False,
108+
raise_error: bool = True,
109109
) -> dict[str, Any]:
110110
"""
111111
Convert an exception to a structured error response.
112112
113113
This function maps common exception types to appropriate error codes
114-
and creates informative error responses.
114+
and creates informative error responses. By default, it raises a ToolError
115+
to signal the error at the MCP protocol level (isError=true).
115116
116117
Args:
117118
error: The exception to convert
118119
context: Additional context to include in the response
119-
raise_error: If True, raises ToolError with the structured error.
120-
If False (default), returns the error dict.
121-
122-
NOTE: The default will change to True in a future PR once
123-
all tools are updated to use ToolError. New code should
124-
explicitly pass raise_error=True for forward compatibility.
120+
raise_error: If True (default), raises ToolError with the structured error.
121+
If False, returns the error dict for further modification.
125122
126123
Returns:
127124
Structured error response dictionary (only if raise_error=False)
128125
129126
Raises:
130-
ToolError: If raise_error=True, raises with JSON-serialized error
127+
ToolError: If raise_error=True (default), raises with JSON-serialized error
131128
"""
132129
error_str = str(error).lower()
133130
error_msg = str(error)

tests/src/unit/test_tool_error_signaling.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,10 @@ def test_preserves_all_error_fields(self):
7575
class TestExceptionToStructuredError:
7676
"""Tests for the exception_to_structured_error function."""
7777

78-
def test_returns_dict_by_default(self):
79-
"""exception_to_structured_error should return dict by default."""
80-
result = exception_to_structured_error(ValueError("test error"))
81-
82-
assert isinstance(result, dict)
83-
assert result["success"] is False
84-
assert "error" in result
85-
86-
def test_raises_tool_error_when_explicit(self):
87-
"""exception_to_structured_error should raise ToolError when raise_error=True."""
78+
def test_raises_tool_error_by_default(self):
79+
"""exception_to_structured_error should raise ToolError by default."""
8880
with pytest.raises(ToolError):
89-
exception_to_structured_error(ValueError("test error"), raise_error=True)
81+
exception_to_structured_error(ValueError("test error"))
9082

9183
def test_returns_dict_when_raise_error_false(self):
9284
"""exception_to_structured_error should return dict when raise_error=False."""

0 commit comments

Comments
 (0)