Skip to main content

gopher_mcp_rust/
error.rs

1//! Error types for the gopher-orch SDK.
2
3use std::error::Error as StdError;
4use std::fmt;
5
6/// Result type alias for gopher-orch operations.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Error types for gopher-orch operations.
10#[derive(Debug)]
11pub enum Error {
12    /// Error loading or using the native library.
13    Library(String),
14
15    /// Error creating an agent.
16    Agent(String),
17
18    /// Invalid API key error.
19    ApiKey(String),
20
21    /// Connection error.
22    Connection(String),
23
24    /// Timeout error.
25    Timeout(String),
26
27    /// Configuration error.
28    Config(String),
29
30    /// Agent has been disposed.
31    Disposed,
32
33    /// Authentication error (gopher-auth).
34    #[cfg(feature = "auth")]
35    Auth(String),
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Error::Library(msg) => write!(f, "Library error: {}", msg),
42            Error::Agent(msg) => write!(f, "Agent error: {}", msg),
43            Error::ApiKey(msg) => write!(f, "API key error: {}", msg),
44            Error::Connection(msg) => write!(f, "Connection error: {}", msg),
45            Error::Timeout(msg) => write!(f, "Timeout error: {}", msg),
46            Error::Config(msg) => write!(f, "Configuration error: {}", msg),
47            Error::Disposed => write!(f, "Agent has been disposed"),
48            #[cfg(feature = "auth")]
49            Error::Auth(msg) => write!(f, "Auth error: {}", msg),
50        }
51    }
52}
53
54impl StdError for Error {}
55
56impl Error {
57    /// Create a new agent error.
58    pub fn agent<S: Into<String>>(msg: S) -> Self {
59        Error::Agent(msg.into())
60    }
61
62    /// Create a new library error.
63    pub fn library<S: Into<String>>(msg: S) -> Self {
64        Error::Library(msg.into())
65    }
66
67    /// Create a new connection error.
68    pub fn connection<S: Into<String>>(msg: S) -> Self {
69        Error::Connection(msg.into())
70    }
71
72    /// Create a new timeout error.
73    pub fn timeout<S: Into<String>>(msg: S) -> Self {
74        Error::Timeout(msg.into())
75    }
76
77    /// Create a new config error.
78    pub fn config<S: Into<String>>(msg: S) -> Self {
79        Error::Config(msg.into())
80    }
81
82    /// Create a new auth error.
83    #[cfg(feature = "auth")]
84    pub fn auth<S: Into<String>>(msg: S) -> Self {
85        Error::Auth(msg.into())
86    }
87}