arcade-mcp/libs/tests/core/test_errors.py
Francisco Or Something 1492c80fc5
TOO-627: Improve error messages for agents and Datadog (#814)
## Summary

- Improve tool call error messages across 4 libraries (arcade-core,
arcade-tdk, arcade-mcp-server, arcade-serve) so agents can self-correct
and Datadog can facet on structured fields
- Guard empty error messages, enrich input validation errors with
field-level detail, fix `@tool` decorator fallback formatting, surface
`additional_prompt_content` in MCP responses, and add structured log
extras for Datadog
- Addresses the 3 worst error patterns: generic "Error in tool input
deserialization", bare `KeyError` values, and empty `FatalToolError`
messages

**Linear:** TOO-627
**Plan:** `docs/plans/2026-04-08-improve-error-messages-handoff.md`

## Tasks

- [ ] Task 1: Guard empty error messages (arcade-core)
- [ ] Task 2: Enrich input validation error messages (arcade-core)
- [ ] Task 3: Improve `@tool` decorator error fallback (arcade-tdk)
- [ ] Task 4: Fix MCP agent-facing error response (arcade-mcp-server)
- [ ] Task 5: Add structured log extras in BaseWorker (arcade-serve)
- [ ] Task 6: Add structured log extras in MCP server
(arcade-mcp-server)

## Test plan

- [ ] Each task has dedicated unit tests verifying the new behavior
- [ ] `make test` passes after all tasks
- [ ] `make check` (ruff + mypy) passes
- [ ] Verify the 3 worst error patterns now produce actionable messages

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Touches cross-library error formatting and logging behavior used in
production tool execution paths; while mostly additive/guardrails, it
changes agent-visible messages and Datadog log facets, which could
impact client expectations and alerting.
> 
> **Overview**
> Improves tool-call error handling across core/runtime, MCP transport,
worker transport, and the TDK to make agent-visible failures more
actionable while *reducing sensitive-data leakage*.
> 
> In `arcade-core`, empty error messages now get placeholders,
`ToolOutputFactory.fail*` defaults blank messages, and input validation
errors are rewritten as field-level summaries that intentionally omit
rejected values (avoiding Pydantic echo of secrets). The `@tool`
fallback in `arcade-tdk` no longer surfaces `str(exception)` to agents;
it returns exception *type-only* in `message` while preserving full
detail in `developer_message`.
> 
> Adds a shared `build_tool_error_log_extra` helper and updates
`arcade-serve` + `arcade-mcp-server` to emit consistent structured
WARNING logs (`error_*`, `tool_name`, optional toolkit/version) for
Datadog, while MCP error responses now append
`additional_prompt_content` and force `structuredContent=None` on
failures per spec. Includes extensive new tests and bumps package
versions (`arcade-core` 4.6.2, `arcade-tdk` 3.6.1, `arcade-mcp-server`
1.19.3, `arcade-serve` 3.2.3).
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
e5c7ebcaf56176cfbd8e6d1f2b6295352abd0ec0. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 20:10:51 -03:00

47 lines
2 KiB
Python

"""Tests for arcade_core.errors.
Covers the empty-message guard in ``ToolkitError.with_context()`` — without it,
``raise FatalToolError("")`` produces prefixed text like ``"...tool 'foo': "``
that carries no diagnostic payload in logs/agent output.
"""
import pytest
from arcade_core.errors import FatalToolError, RetryableToolError, ToolkitLoadError
@pytest.mark.parametrize("empty_message", ["", " ", "\t", "\n \n"])
def test_with_context_empty_message_substitutes_placeholder(empty_message):
err = FatalToolError(empty_message).with_context("my_tool")
# Prefix is preserved — kind, error type and tool name are still in the message.
assert "[TOOL_RUNTIME_FATAL]" in err.message
assert "my_tool" in err.message
# And the empty body is replaced with a recognizable placeholder so the
# message ends with diagnostic content rather than ``": "``.
assert "(no details provided)" in err.message
assert not err.message.endswith(": ")
def test_with_context_nonempty_message_unchanged():
err = FatalToolError("Spreadsheet not found").with_context("get_sheet")
assert err.message.endswith(": Spreadsheet not found")
assert "(no details provided)" not in err.message
def test_with_context_developer_message_with_empty_message_still_works():
# A non-empty developer_message is preserved alongside the placeholder body.
err = FatalToolError("", developer_message="trace: foo.py:42").with_context("my_tool")
assert "(no details provided)" in err.message
assert err.developer_message is not None
assert err.developer_message.endswith(": trace: foo.py:42")
def test_with_context_retryable_error_empty_message():
err = RetryableToolError(" ").with_context("flaky_tool")
assert "[TOOL_RUNTIME_RETRY]" in err.message
assert "(no details provided)" in err.message
def test_with_context_toolkit_load_error_empty_message():
err = ToolkitLoadError("").with_context("broken_toolkit")
assert "broken_toolkit" in err.message
assert "(no details provided)" in err.message