avassa_client/
error.rs

1/// Description of an error from the REST APIs
2#[derive(Debug,serde::Deserialize)]
3pub struct RESTError {
4    /// Error message
5    #[serde(rename = "error-message")]
6    pub error_message: String,
7    /// Additonal error information
8    #[serde(rename = "error-info")]
9    pub error_info: serde_json::Value,
10}
11
12/// List of REST API error messages
13#[derive(Debug,serde::Deserialize)]
14pub struct RESTErrorList {
15    /// Error messages
16    pub errors: Vec<RESTError>,
17}
18
19/// Error returned by client functions
20#[derive(Debug, thiserror::Error)]
21#[non_exhaustive]
22pub enum Error {
23    /// Login failed
24    #[error("Login failed: {0}")]
25    LoginFailure(String),
26
27    /// Application login failed because an environment variable is missing
28    #[error("Login failure, missing environment variable '{0}'")]
29    LoginFailureMissingEnv(String),
30
31    /// Failed returned by the HTTP server
32    #[error("HTTP failed {0}, {1} - {2}")]
33    WebServer(u16, String, String),
34
35    /// Websocket error
36    #[cfg(feature = "client")]
37    #[error("Websocket error: {0}")]
38    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
39
40    /// JSON serialization/deserialization error
41    #[error("Serde JSON error: {0}")]
42    Serde(#[from] serde_json::Error),
43
44    /// URL parsing error
45    #[cfg(feature = "client")]
46    #[error("URL: {0}")]
47    URL(#[from] url::ParseError),
48
49    /// HTTP client error
50    #[cfg(feature = "client")]
51    #[error("Reqwest: {0}")]
52    HTTPClient(#[from] reqwest::Error),
53
54    /// Volga error
55    #[error("Error from Volga {0:?}")]
56    Volga(Option<String>),
57
58    /// This error is returned if we get data from the API we can't parse/understand
59    #[error("API Error {0:?}")]
60    API(String),
61
62    /// This error is returned from the REST API, this typically means the client did something
63    /// wrong.
64    #[error("REST error {0:?}")]
65    REST(RESTErrorList),
66
67    /// TLS Errors
68    #[cfg(feature = "client")]
69    #[error("TLS error {0}")]
70    TLS(#[from] tokio_rustls::rustls::Error),
71
72    /// DNS Error
73    #[cfg(feature = "client")]
74    #[error("DNSName error {0}")]
75    DNSName(#[from] tokio_rustls::rustls::pki_types::InvalidDnsNameError),
76
77    /// IO Errors
78    #[error("IO error {0}")]
79    IO(#[from] std::io::Error),
80
81    /// General Error
82    #[error("Error {0}")]
83    General(String),
84
85    /// Convert web socket URL to URI
86    #[error("Conversoin error {0}")]
87    InvalidURI(#[from] http::uri::InvalidUri),
88}
89
90impl Error {
91    /// Create a general error
92    #[must_use]
93    pub fn general(err: &str) -> Self {
94        Self::General(err.to_string())
95    }
96}
97
98impl From<tokio_tungstenite::tungstenite::Error> for Error {
99    fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
100        Self::WebSocket(Box::new(err))
101    }
102}
103
104/// Result type
105pub type Result<T> = std::result::Result<T, Error>;
106