rustyclaw_core/error.rs
1//! Error handling utilities for RustyClaw.
2//!
3//! ## Error handling patterns
4//!
5//! RustyClaw uses two error handling strategies:
6//!
7//! 1. **Tools** (`Result<String, String>`): Tools return simple string errors
8//! because these are sent back to the AI model. The error message is displayed
9//! to the model which can then try to recover or report the issue to the user.
10//!
11//! 2. **Application logic** (`anyhow::Result`): Internal application code uses
12//! `anyhow` for its rich error context and easy propagation.
13//!
14//! ## Converting between error types
15//!
16//! Use the `anyhow_to_tool_err` and `tool_err_to_anyhow` functions to convert
17//! between the two error types when needed.
18
19use anyhow::Result as AnyhowResult;
20
21/// Convert an anyhow error to a tool error (string message).
22pub fn anyhow_to_tool_err(err: anyhow::Error) -> String {
23 err.to_string()
24}
25
26/// Convert an anyhow result to a tool result.
27pub fn anyhow_to_tool_result<T>(result: AnyhowResult<T>) -> Result<T, String> {
28 result.map_err(anyhow_to_tool_err)
29}
30
31/// Convert a tool error to an anyhow error.
32pub fn tool_err_to_anyhow(err: String) -> anyhow::Error {
33 anyhow::anyhow!(err)
34}
35
36/// Convert a tool result to an anyhow result.
37pub fn tool_to_anyhow_result<T>(result: Result<T, String>) -> AnyhowResult<T> {
38 result.map_err(tool_err_to_anyhow)
39}