axcp/
error.rs

1//! Error handling for the AXCP Rust SDK.
2
3use thiserror::Error;
4
5/// A type alias for `Result<T, Error>`.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// The error type for the AXCP SDK.
9#[derive(Debug, Error)]
10pub enum Error {
11    /// Represents a configuration error.
12    #[error("Configuration error: {0}")]
13    Config(String),
14
15    /// Represents an I/O error.
16    #[error("I/O error: {source}")]
17    Io {
18        /// The source of the I/O error.
19        #[from]
20        source: std::io::Error,
21    },
22
23    /// Represents a network error.
24    #[error("Network error: {0}")]
25    Network(String),
26
27    /// Represents a serialization/deserialization error.
28    #[error("Serialization error: {0}")]
29    Serialization(String),
30
31    /// Represents a protocol error.
32    #[error("Protocol error: {0}")]
33    Protocol(String),
34
35    /// Represents an authentication error.
36    #[error("Authentication failed: {0}")]
37    Auth(String),
38
39    /// Represents a timeout error.
40    #[error("Operation timed out")]
41    Timeout,
42
43    /// Represents an error from the server.
44    #[error("Server error: {0}")]
45    Server(String),
46
47
48    /// Represents any other kind of error.
49    #[error("Unexpected error: {0}")]
50    Other(String),
51}
52
53impl From<reqwest::Error> for Error {
54    fn from(err: reqwest::Error) -> Self {
55        if err.is_timeout() {
56            Error::Timeout
57        } else if err.is_connect() {
58            Error::Network(format!("Connection error: {}", err))
59        } else if err.is_decode() {
60            Error::Serialization(format!("Failed to decode response: {}", err))
61        } else {
62            Error::Network(err.to_string())
63        }
64    }
65}
66
67impl From<serde_json::Error> for Error {
68    fn from(err: serde_json::Error) -> Self {
69        Error::Serialization(err.to_string())
70    }
71}
72
73impl From<url::ParseError> for Error {
74    fn from(err: url::ParseError) -> Self {
75        Error::Config(format!("Invalid URL: {}", err))
76    }
77}