Skip to main content

aimdb_client/
error.rs

1//! Error types for AimDB client library
2
3use thiserror::Error;
4
5/// Result type for client operations
6pub type ClientResult<T> = Result<T, ClientError>;
7
8/// Errors that can occur during client operations
9#[derive(Error, Debug)]
10pub enum ClientError {
11    /// No AimDB instances found during discovery
12    #[error("No running AimDB instances found")]
13    NoInstancesFound,
14
15    /// Connection error
16    #[error("Connection failed to {socket}: {reason}")]
17    ConnectionFailed { socket: String, reason: String },
18
19    /// Server returned an error
20    #[error("Server error (code {code}): {message}")]
21    ServerError {
22        code: String,
23        message: String,
24        details: Option<serde_json::Value>,
25    },
26
27    /// I/O error
28    #[error("I/O error: {0}")]
29    Io(#[from] std::io::Error),
30
31    /// JSON serialization/deserialization error
32    #[error("JSON error: {0}")]
33    Json(#[from] serde_json::Error),
34
35    /// Generic error
36    #[error("{0}")]
37    Other(#[from] anyhow::Error),
38}
39
40impl ClientError {
41    /// Create a connection failed error
42    pub fn connection_failed(socket: impl Into<String>, reason: impl Into<String>) -> Self {
43        Self::ConnectionFailed {
44            socket: socket.into(),
45            reason: reason.into(),
46        }
47    }
48
49    /// Create a server error
50    pub fn server_error(
51        code: impl Into<String>,
52        message: impl Into<String>,
53        details: Option<serde_json::Value>,
54    ) -> Self {
55        Self::ServerError {
56            code: code.into(),
57            message: message.into(),
58            details,
59        }
60    }
61}