concord_client/
error.rs

1use tokio_tungstenite::tungstenite;
2
3#[derive(Debug)]
4pub struct ApiError {
5    pub message: String,
6}
7
8impl ApiError {
9    pub fn simple(message: &str) -> Self {
10        ApiError {
11            message: message.to_string(),
12        }
13    }
14}
15
16#[macro_export]
17macro_rules! api_error {
18    ($($arg:tt)*) => {
19        ApiError {
20            message: format!($($arg)*),
21        }
22    };
23}
24
25#[macro_export]
26macro_rules! api_err {
27    ($($arg:tt)*) => {
28        Err($crate::api_error!($($arg)*))
29    };
30}
31
32impl std::error::Error for ApiError {}
33
34impl std::fmt::Display for ApiError {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(f, "{}", self.message)
37    }
38}
39
40impl From<tungstenite::Error> for ApiError {
41    fn from(e: tungstenite::Error) -> Self {
42        ApiError {
43            message: e.to_string(),
44        }
45    }
46}
47
48impl From<serde_json::Error> for ApiError {
49    fn from(e: serde_json::Error) -> Self {
50        ApiError {
51            message: e.to_string(),
52        }
53    }
54}
55
56impl From<reqwest::Error> for ApiError {
57    fn from(e: reqwest::Error) -> Self {
58        ApiError {
59            message: e.to_string(),
60        }
61    }
62}
63
64impl From<url::ParseError> for ApiError {
65    fn from(e: url::ParseError) -> Self {
66        ApiError {
67            message: e.to_string(),
68        }
69    }
70}