1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum RuntimeError {
5 #[error("API error: {0}")]
6 Api(#[from] reqwest::Error),
7 #[error("Auth error: {0}")]
8 Auth(String),
9 #[error("Config error: {0}")]
10 Config(String),
11 #[error("Session error: {0}")]
12 Session(String),
13 #[error("Tool execution failed: {0}")]
14 Tool(String),
15 #[error("Request timed out")]
16 Timeout,
17 #[error("Operation canceled")]
18 Canceled,
19}
20
21pub type Result<T> = std::result::Result<T, RuntimeError>;
22
23#[cfg(test)]
24mod tests {
25 use super::*;
26
27 #[test]
28 fn test_runtime_error_display() {
29 assert_eq!(
30 format!("{}", RuntimeError::Auth("bad token".into())),
31 "Auth error: bad token"
32 );
33
34 assert_eq!(
35 format!("{}", RuntimeError::Config("missing".into())),
36 "Config error: missing"
37 );
38
39 assert_eq!(
40 format!("{}", RuntimeError::Tool("failed".into())),
41 "Tool execution failed: failed"
42 );
43
44 assert_eq!(
45 format!("{}", RuntimeError::Session("not found".into())),
46 "Session error: not found"
47 );
48
49 assert_eq!(
50 format!("{}", RuntimeError::Timeout),
51 "Request timed out"
52 );
53
54 assert_eq!(
55 format!("{}", RuntimeError::Canceled),
56 "Operation canceled"
57 );
58 }
59
60 #[test]
61 fn test_runtime_error_to_string() {
62 assert_eq!(
63 RuntimeError::Auth("bad token".into()).to_string(),
64 "Auth error: bad token"
65 );
66
67 assert_eq!(
68 RuntimeError::Config("missing".into()).to_string(),
69 "Config error: missing"
70 );
71
72 assert_eq!(
73 RuntimeError::Tool("failed".into()).to_string(),
74 "Tool execution failed: failed"
75 );
76
77 assert_eq!(
78 RuntimeError::Session("not found".into()).to_string(),
79 "Session error: not found"
80 );
81
82 assert_eq!(
83 RuntimeError::Timeout.to_string(),
84 "Request timed out"
85 );
86
87 assert_eq!(
88 RuntimeError::Canceled.to_string(),
89 "Operation canceled"
90 );
91 }
92}