Skip to main content

threexui_rs/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum Error {
5    #[error("not authenticated — call login() first")]
6    NotAuthenticated,
7
8    #[error("authentication failed: {0}")]
9    Auth(String),
10
11    #[error("api error: {0}")]
12    Api(String),
13
14    #[error("endpoint not found: {0} (panel version may be too old)")]
15    EndpointNotFound(String),
16
17    #[error("http error: {0}")]
18    Http(#[from] reqwest::Error),
19
20    #[error("json error: {0}")]
21    Json(#[from] serde_json::Error),
22
23    #[error("invalid config: {0}")]
24    Config(String),
25}
26
27pub type Result<T> = std::result::Result<T, Error>;
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn error_display_not_authenticated() {
35        let e = Error::NotAuthenticated;
36        assert_eq!(e.to_string(), "not authenticated — call login() first");
37    }
38
39    #[test]
40    fn error_display_api() {
41        let e = Error::Api("bad request".to_string());
42        assert_eq!(e.to_string(), "api error: bad request");
43    }
44
45    #[test]
46    fn error_display_endpoint_not_found() {
47        let e = Error::EndpointNotFound("/panel/api/inbounds/X/copyClients".to_string());
48        assert!(e.to_string().contains("endpoint not found"));
49        assert!(e.to_string().contains("copyClients"));
50    }
51
52    #[test]
53    fn error_display_config() {
54        let e = Error::Config("port cannot be 0".to_string());
55        assert_eq!(e.to_string(), "invalid config: port cannot be 0");
56    }
57}