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 {endpoint}: {reason}")]
17    ConnectionFailed { endpoint: String, reason: String },
18
19    /// The endpoint string was malformed, or named a `scheme://` whose transport
20    /// is not compiled into this build.
21    #[error("Unsupported endpoint {endpoint:?}: {reason}")]
22    UnsupportedEndpoint { endpoint: String, reason: String },
23
24    /// Server returned an error
25    #[error("Server error (code {code}): {message}")]
26    ServerError {
27        code: String,
28        message: String,
29        details: Option<serde_json::Value>,
30    },
31
32    /// I/O error
33    #[error("I/O error: {0}")]
34    Io(#[from] std::io::Error),
35
36    /// JSON serialization/deserialization error
37    #[error("JSON error: {0}")]
38    Json(#[from] serde_json::Error),
39
40    /// Generic error
41    #[error("{0}")]
42    Other(#[from] anyhow::Error),
43}
44
45impl ClientError {
46    /// Create a connection failed error
47    pub fn connection_failed(endpoint: impl Into<String>, reason: impl Into<String>) -> Self {
48        Self::ConnectionFailed {
49            endpoint: endpoint.into(),
50            reason: reason.into(),
51        }
52    }
53
54    /// Create an unsupported-endpoint error (bad URL, or a scheme not built in).
55    pub fn unsupported_endpoint(endpoint: impl Into<String>, reason: impl Into<String>) -> Self {
56        Self::UnsupportedEndpoint {
57            endpoint: endpoint.into(),
58            reason: reason.into(),
59        }
60    }
61
62    /// Create a server error
63    pub fn server_error(
64        code: impl Into<String>,
65        message: impl Into<String>,
66        details: Option<serde_json::Value>,
67    ) -> Self {
68        Self::ServerError {
69            code: code.into(),
70            message: message.into(),
71            details,
72        }
73    }
74}