Skip to main content

anyclaw_sdk_tool/
error.rs

1/// Errors produced by tool SDK operations.
2#[derive(Debug, thiserror::Error)]
3pub enum ToolSdkError {
4    /// Tool logic failed with a user-facing message.
5    #[error("Tool execution failed: {0}")]
6    ExecutionFailed(String),
7    /// The caller supplied invalid input to the tool.
8    #[error("Invalid input: {0}")]
9    InvalidInput(String),
10    /// An I/O error occurred during tool execution.
11    #[error("IO error: {0}")]
12    Io(#[from] std::io::Error),
13    /// JSON serialization or deserialization failed.
14    #[error("Serialization error: {0}")]
15    Serde(#[from] serde_json::Error),
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21    use rstest::rstest;
22
23    #[rstest]
24    fn when_execution_failed_displayed_then_contains_message() {
25        let err = ToolSdkError::ExecutionFailed("timeout".into());
26        assert_eq!(err.to_string(), "Tool execution failed: timeout");
27    }
28
29    #[rstest]
30    fn when_invalid_input_displayed_then_contains_message() {
31        let err = ToolSdkError::InvalidInput("missing required field 'path'".into());
32        assert_eq!(
33            err.to_string(),
34            "Invalid input: missing required field 'path'"
35        );
36    }
37
38    #[rstest]
39    fn when_io_error_converted_then_displays_io_error() {
40        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
41        let err: ToolSdkError = io_err.into();
42        assert!(matches!(err, ToolSdkError::Io(_)));
43        assert!(err.to_string().contains("file not found"));
44    }
45
46    #[rstest]
47    fn when_serde_error_converted_then_displays_serialization_error() {
48        let json_err = serde_json::from_str::<serde_json::Value>("bad json").unwrap_err();
49        let err: ToolSdkError = json_err.into();
50        assert!(matches!(err, ToolSdkError::Serde(_)));
51        assert!(err.to_string().starts_with("Serialization error:"));
52    }
53
54    #[rstest]
55    fn when_tool_sdk_error_checked_then_implements_std_error() {
56        let err = ToolSdkError::ExecutionFailed("test".into());
57        let _: &dyn std::error::Error = &err;
58    }
59}