Expand description
Common error utilities for agent tools
This module provides shared error handling infrastructure without replacing individual tool error types. Each tool keeps its own error type (e.g., ReadFileError, ShellError) but uses these utilities for consistent formatting.
§Pattern
Tools should:
- Keep their own error type deriving
thiserror::Error - Use
ToolErrorContexttrait to add context when propagating errors - Use
format_error_for_llmwhen returning error JSON to the agent
§Example
ⓘ
use crate::agent::tools::error::{ToolErrorContext, ErrorCategory, format_error_for_llm};
fn read_config(&self, path: &Path) -> Result<String, ReadFileError> {
fs::read_to_string(path)
.with_tool_context("read_file", "reading configuration file")
.map_err(|e| ReadFileError(e))
}
// In tool call, for JSON error responses:
let error_json = format_error_for_llm(
"read_file",
ErrorCategory::FileNotFound,
"File not found: config.yaml",
Some(vec!["Check if the file exists", "Verify the path is correct"]),
);Enums§
- Error
Category - Common error categories for tool errors
Traits§
- Tool
Error Context - Extension trait for adding tool context to errors
Functions§
- detect_
error_ category - Helper to detect error category from common error patterns
- format_
error_ for_ llm - Format an error for LLM consumption
- format_
error_ with_ context - Format an error with additional context fields