1use thiserror::Error;
4
5pub type ClientResult<T> = Result<T, ClientError>;
7
8#[derive(Error, Debug)]
10pub enum ClientError {
11 #[error("No running AimDB instances found")]
13 NoInstancesFound,
14
15 #[error("Connection failed to {endpoint}: {reason}")]
17 ConnectionFailed { endpoint: String, reason: String },
18
19 #[error("Unsupported endpoint {endpoint:?}: {reason}")]
22 UnsupportedEndpoint { endpoint: String, reason: String },
23
24 #[error("Server error (code {code}): {message}")]
26 ServerError {
27 code: String,
28 message: String,
29 details: Option<serde_json::Value>,
30 },
31
32 #[error("I/O error: {0}")]
34 Io(#[from] std::io::Error),
35
36 #[error("JSON error: {0}")]
38 Json(#[from] serde_json::Error),
39
40 #[error("{0}")]
42 Other(#[from] anyhow::Error),
43}
44
45impl ClientError {
46 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 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 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}