ts3_clientquery_lib/
error.rs1mod query {
2 use crate::types::QueryStatus;
3 use std::fmt::{Display, Formatter};
4 use std::io::Error;
5
6 #[derive(Clone, Default, Debug)]
7 pub struct QueryError {
8 code: i32,
9 message: String,
10 }
11
12 impl QueryError {
13 pub fn code(&self) -> i32 {
14 self.code
15 }
16 pub fn message(&self) -> &str {
17 &self.message
18 }
19
20 pub fn static_empty_response() -> Self {
21 Self {
22 code: -1,
23 message: "Expect result but none found.".to_string(),
24 }
25 }
26
27 pub fn send_message_error(data: String) -> Self {
28 Self {
29 code: -2,
30 message: format!("Unable to send message, raw data => {}", data),
31 }
32 }
33
34 pub fn decode_error(data: &str) -> Self {
35 Self {
36 code: -3,
37 message: format!("Decode result error: {}", data),
38 }
39 }
40
41 pub fn length_mismatch(payload: &str, size: usize) -> Self {
42 Self {
43 code: -4,
44 message: format!(
45 "Error payload size mismatch! expect {} but {} found. payload: {:?}",
46 payload.as_bytes().len(),
47 size,
48 payload
49 ),
50 }
51 }
52
53 pub fn except_data_not_found() -> Self {
54 Self {
55 code: -5,
56 message: "Except data but not found".to_string(),
57 }
58 }
59
60 pub fn except_data_not_found_payload(payload: &str) -> Self {
61 Self {
62 code: -5,
63 message: format!("Except data but not found, payload => {:?}", payload),
64 }
65 }
66
67 pub fn parse_error(e: serde_teamspeak_querystring::Error, line: &str) -> Self {
68 Self {
69 code: -7,
70 message: format!("ParseError {:?} {:?}", line, e),
71 }
72 }
73
74 pub fn split_error(line: &str) -> Self {
75 Self {
76 code: -7,
77 message: format!("SplitError: {:?}", line),
78 }
79 }
80
81 pub fn deserialize_error(e: serde_teamspeak_querystring::Error) -> Self {
82 Self {
83 code: -7,
84 message: format!("DeserializeError: {:?}", e),
85 }
86 }
87 }
88
89 impl From<Error> for QueryError {
90 fn from(value: Error) -> Self {
91 Self {
92 code: -6,
93 message: format!("IOError: {:?}", value),
94 }
95 }
96 }
97
98 impl From<serde_teamspeak_querystring::Error> for QueryError {
99 fn from(value: serde_teamspeak_querystring::Error) -> Self {
100 QueryError::deserialize_error(value)
101 }
102 }
103
104 #[non_exhaustive]
105 #[derive(Clone, Debug)]
106 pub enum ErrorKind {
107 EmptyResponse,
108 SendMessageError,
109 DecodeError,
110 LengthMismatch,
111 EmptyResultResponse,
112 IOError,
113 DeserializeError,
114 TeamSpeakError,
115 OK,
116 }
117
118 impl From<QueryError> for ErrorKind {
119 fn from(value: QueryError) -> Self {
120 match value.code() {
121 0 => ErrorKind::OK,
122 -1 => ErrorKind::EmptyResponse,
123 -2 => ErrorKind::SendMessageError,
124 -3 => ErrorKind::DecodeError,
125 -4 => ErrorKind::LengthMismatch,
126 -5 => ErrorKind::EmptyResultResponse,
127 -6 => ErrorKind::IOError,
128 -7 => ErrorKind::DeserializeError,
129 1.. => ErrorKind::TeamSpeakError,
130 _ => unreachable!("Should add more error kind to this enum"),
131 }
132 }
133 }
134
135 impl Display for QueryError {
136 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
137 write!(f, "{}({})", self.message, self.code)
138 }
139 }
140
141 impl std::error::Error for QueryError {}
142
143 impl From<QueryStatus> for QueryError {
144 fn from(status: QueryStatus) -> Self {
145 Self {
146 code: status.id(),
147 message: status.msg().clone(),
148 }
149 }
150 }
151
152 }
161
162pub use query::{ErrorKind, QueryError};
163pub type QueryResult<T> = Result<T, QueryError>;