1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
10pub enum Error {
11 #[error("missing required parameter: {0}")]
13 MissingParameter(String),
14
15 #[error("invalid parameter type for '{name}': expected {expected}, got {actual}")]
17 InvalidParameterType {
18 name: String,
20 expected: String,
22 actual: String,
24 },
25
26 #[error("tool not found: {0}")]
28 ToolNotFound(String),
29
30 #[error("tool execution failed: {0}")]
32 ExecutionFailed(String),
33
34 #[error("serialization error: {0}")]
36 Serialization(#[from] serde_json::Error),
37
38 #[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}