asterisk_ari/
errors.rs

1use reqwest::header::InvalidHeaderValue;
2use reqwest::Error as ReqwError;
3use reqwest::StatusCode;
4use std::result;
5use tokio_tungstenite::tungstenite::Error as WSError;
6use url::ParseError;
7
8/// Represents various errors that can occur in the ARI client.
9#[derive(Debug)]
10pub enum AriError {
11    /// Error during JSON serialization/deserialization.
12    Serde(serde_json::Error),
13    /// Error converting a byte array to a UTF-8 string.
14    Utf8(std::string::FromUtf8Error),
15    /// API-specific error.
16    Api(ApiError),
17    /// Invalid HTTP header value.
18    HttpInvalidHeader(InvalidHeaderValue),
19    /// General HTTP error.
20    Http(ReqwError),
21    /// URL parsing error.
22    UrlParse(ParseError),
23    /// WebSocket error.
24    Websocket(WSError),
25}
26
27impl AriError {
28    /// Creates a new `AriError` with the given status code and optional content.
29    ///
30    /// # Arguments
31    ///
32    /// * `code` - The HTTP status code.
33    /// * `content` - Optional content associated with the error.
34    ///
35    /// # Returns
36    ///
37    /// A new instance of `AriError`.
38    pub fn new(code: StatusCode, content: Option<String>) -> Self {
39        AriError::Api(ApiError { code, content })
40    }
41}
42
43/// Result type alias for operations that can return an `AriError`.
44pub type Result<T> = result::Result<T, AriError>;
45
46/// Represents an API-specific error.
47#[derive(Debug)]
48pub struct ApiError {
49    /// The HTTP status code associated with the error.
50    pub code: StatusCode,
51    /// Optional content associated with the error.
52    pub content: Option<String>,
53}
54
55impl From<serde_json::Error> for AriError {
56    /// Converts a `serde_json::Error` into an `AriError`.
57    ///
58    /// # Arguments
59    ///
60    /// * `e` - The `serde_json::Error` to convert.
61    ///
62    /// # Returns
63    ///
64    /// An `AriError` representing the JSON error.
65    fn from(e: serde_json::Error) -> Self {
66        AriError::Serde(e)
67    }
68}
69
70impl From<std::string::FromUtf8Error> for AriError {
71    /// Converts a `std::string::FromUtf8Error` into an `AriError`.
72    ///
73    /// # Arguments
74    ///
75    /// * `e` - The `std::string::FromUtf8Error` to convert.
76    ///
77    /// # Returns
78    ///
79    /// An `AriError` representing the UTF-8 conversion error.
80    fn from(e: std::string::FromUtf8Error) -> Self {
81        AriError::Utf8(e)
82    }
83}
84
85impl From<ParseError> for AriError {
86    /// Converts a `ParseError` into an `AriError`.
87    ///
88    /// # Arguments
89    ///
90    /// * `e` - The `ParseError` to convert.
91    ///
92    /// # Returns
93    ///
94    /// An `AriError` representing the URL parsing error.
95    fn from(e: ParseError) -> Self {
96        AriError::UrlParse(e)
97    }
98}
99
100impl From<WSError> for AriError {
101    /// Converts a `WSError` into an `AriError`.
102    ///
103    /// # Arguments
104    ///
105    /// * `e` - The `WSError` to convert.
106    ///
107    /// # Returns
108    ///
109    /// An `AriError` representing the WebSocket error.
110    fn from(e: WSError) -> Self {
111        AriError::Websocket(e)
112    }
113}
114
115impl From<InvalidHeaderValue> for AriError {
116    /// Converts an `InvalidHeaderValue` into an `AriError`.
117    ///
118    /// # Arguments
119    ///
120    /// * `e` - The `InvalidHeaderValue` to convert.
121    ///
122    /// # Returns
123    ///
124    /// An `AriError` representing the invalid HTTP header value error.
125    fn from(e: InvalidHeaderValue) -> Self {
126        AriError::HttpInvalidHeader(e)
127    }
128}
129
130impl From<ReqwError> for AriError {
131    /// Converts a `ReqwError` into an `AriError`.
132    ///
133    /// # Arguments
134    ///
135    /// * `e` - The `ReqwError` to convert.
136    ///
137    /// # Returns
138    ///
139    /// An `AriError` representing the HTTP error.
140    fn from(e: ReqwError) -> Self {
141        AriError::Http(e)
142    }
143}