thulp_core/
error.rs

1//! Error types for thulp-core.
2
3use thiserror::Error;
4
5/// Result type alias using [`Error`].
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur in thulp-core.
9#[derive(Debug, Error)]
10pub enum Error {
11    /// A required parameter was not provided.
12    #[error("missing required parameter: {0}")]
13    MissingParameter(String),
14
15    /// A parameter has an invalid type.
16    #[error("invalid parameter type for '{name}': expected {expected}, got {actual}")]
17    InvalidParameterType {
18        /// Parameter name.
19        name: String,
20        /// Expected type.
21        expected: String,
22        /// Actual type received.
23        actual: String,
24    },
25
26    /// Tool not found.
27    #[error("tool not found: {0}")]
28    ToolNotFound(String),
29
30    /// Tool execution failed.
31    #[error("tool execution failed: {0}")]
32    ExecutionFailed(String),
33
34    /// Serialization error.
35    #[error("serialization error: {0}")]
36    Serialization(#[from] serde_json::Error),
37
38    /// Invalid configuration.
39    #[error("invalid configuration: {0}")]
40    InvalidConfig(String),
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn error_display_missing_parameter() {
49        let err = Error::MissingParameter("username".to_string());
50        assert_eq!(err.to_string(), "missing required parameter: username");
51    }
52
53    #[test]
54    fn error_display_invalid_type() {
55        let err = Error::InvalidParameterType {
56            name: "count".to_string(),
57            expected: "integer".to_string(),
58            actual: "string".to_string(),
59        };
60        assert_eq!(
61            err.to_string(),
62            "invalid parameter type for 'count': expected integer, got string"
63        );
64    }
65
66    #[test]
67    fn error_display_tool_not_found() {
68        let err = Error::ToolNotFound("unknown_tool".to_string());
69        assert_eq!(err.to_string(), "tool not found: unknown_tool");
70    }
71
72    #[test]
73    fn error_from_serde_json() {
74        let json_err: serde_json::Error = serde_json::from_str::<String>("invalid").unwrap_err();
75        let err: Error = json_err.into();
76        assert!(matches!(err, Error::Serialization(_)));
77    }
78}