1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::io;
use thiserror::Error;
#[derive(Debug, PartialOrd, PartialEq)]
pub struct DivoomServerErrorInfo {
pub http_status_code: u16,
pub error_code: i32,
pub error_message: String,
}
impl DivoomServerErrorInfo {
pub fn http_error(http_status_code: u16) -> DivoomServerErrorInfo {
DivoomServerErrorInfo {
http_status_code,
error_code: 0,
error_message: "".to_string(),
}
}
pub fn server_error(error_code: i32) -> DivoomServerErrorInfo {
DivoomServerErrorInfo {
http_status_code: 0,
error_code,
error_message: "".to_string(),
}
}
pub fn server_error_with_message(
error_code: i32,
error_message: String,
) -> DivoomServerErrorInfo {
DivoomServerErrorInfo {
http_status_code: 0,
error_code,
error_message,
}
}
}
#[derive(Debug, Error)]
pub enum DivoomAPIError {
#[error("Invalid parameter.")]
ParameterError(String),
#[error("Failed to load resource")]
ResourceLoadError {
#[from]
source: io::Error,
},
#[error("Failed to decode resource")]
ResourceDecodeError(String),
#[error("Failed to send request")]
RequestError {
#[from]
source: reqwest::Error,
},
#[error("Failed to deserialize the response")]
ResponseDeserializationError {
#[from]
source: serde_json::Error,
},
#[error("Service or device responded failure")]
ServerError(DivoomServerErrorInfo),
}
pub type DivoomAPIResult<T> = std::result::Result<T, DivoomAPIError>;