thinkingdata_core/
error.rs1use std::fmt;
18
19pub type Result<T> = std::result::Result<T, TDError>;
21
22#[derive(Debug, Clone, thiserror::Error)]
28pub enum TDError {
29 #[error("{0}")]
31 Message(String),
32
33 #[error("http request failed: {0}")]
35 Http(String),
36
37 #[error("io operation failed: {0}")]
39 Io(String),
40
41 #[error("json serialization failed: {0}")]
43 Json(String),
44
45 #[error("invalid server url: {0}")]
47 InvalidServerUrl(String),
48
49 #[error("the event name must be provided")]
51 EmptyEventName,
52
53 #[error("the event id must be provided")]
55 EmptyEventId,
56
57 #[error("the 'firstCheckId' must be provided")]
59 EmptyFirstCheckId,
60
61 #[error("invalid event name: {0}")]
63 InvalidEventName(String),
64
65 #[error("invalid property key: {0}")]
68 InvalidPropertyKey(String),
69
70 #[error("invalid property value: only numbers is supported by UserAdd")]
72 InvalidUserAddValue,
73
74 #[error("invalid parameters: account_id and distinct_id cannot be empty at the same time")]
76 EmptyIds,
77
78 #[error("invalid params for UserUnset: keys is nil")]
80 EmptyUnsetKeys,
81
82 #[error("batch consumer has been closed")]
84 ConsumerClosed,
85}
86
87impl From<reqwest::Error> for TDError {
88 fn from(err: reqwest::Error) -> Self {
89 TDError::Http(err.to_string())
90 }
91}
92
93impl From<std::io::Error> for TDError {
94 fn from(err: std::io::Error) -> Self {
95 TDError::Io(err.to_string())
96 }
97}
98
99impl From<serde_json::Error> for TDError {
100 fn from(err: serde_json::Error) -> Self {
101 TDError::Json(err.to_string())
102 }
103}
104
105impl From<url::ParseError> for TDError {
106 fn from(err: url::ParseError) -> Self {
107 TDError::InvalidServerUrl(err.to_string())
108 }
109}
110
111impl TDError {
112 pub fn msg(message: impl fmt::Display) -> Self {
114 TDError::Message(message.to_string())
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 #[test]
123 fn clone_preserves_message() {
124 let err = TDError::InvalidEventName("bad name".to_string());
125 let cloned = err.clone();
126 assert_eq!(cloned.to_string(), "invalid event name: bad name");
127 }
128
129 #[test]
130 fn from_io_error_wraps_message() {
131 let err: TDError = std::io::Error::new(std::io::ErrorKind::NotFound, "missing").into();
132 assert!(err.to_string().contains("missing"));
133 }
134}