mockforge_analytics/
error.rs1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, AnalyticsError>;
7
8#[derive(Debug, Error)]
10pub enum AnalyticsError {
11 #[error("Database error: {0}")]
13 Database(#[from] sqlx::Error),
14
15 #[error("Migration error: {0}")]
17 Migration(String),
18
19 #[error("Serialization error: {0}")]
21 Serialization(#[from] serde_json::Error),
22
23 #[error("HTTP error: {0}")]
25 Http(#[from] reqwest::Error),
26
27 #[error("Invalid configuration: {0}")]
29 InvalidConfig(String),
30
31 #[error("Query error: {0}")]
33 Query(String),
34
35 #[error("Export error: {0}")]
37 Export(String),
38
39 #[error("IO error: {0}")]
41 Io(#[from] std::io::Error),
42
43 #[error("Invalid input: {0}")]
45 InvalidInput(String),
46
47 #[error("{0}")]
49 Other(String),
50}
51
52impl From<String> for AnalyticsError {
53 fn from(s: String) -> Self {
54 Self::Other(s)
55 }
56}
57
58impl From<&str> for AnalyticsError {
59 fn from(s: &str) -> Self {
60 Self::Other(s.to_string())
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_analytics_error_from_string() {
70 let error: AnalyticsError = "test error".into();
71 assert!(matches!(error, AnalyticsError::Other(_)));
72 assert_eq!(error.to_string(), "test error");
73 }
74
75 #[test]
76 fn test_analytics_error_from_owned_string() {
77 let error: AnalyticsError = String::from("owned error").into();
78 assert!(matches!(error, AnalyticsError::Other(_)));
79 assert_eq!(error.to_string(), "owned error");
80 }
81
82 #[test]
83 fn test_analytics_error_migration() {
84 let error = AnalyticsError::Migration("migration failed".to_string());
85 assert!(error.to_string().contains("Migration error"));
86 assert!(error.to_string().contains("migration failed"));
87 }
88
89 #[test]
90 fn test_analytics_error_invalid_config() {
91 let error = AnalyticsError::InvalidConfig("bad config".to_string());
92 assert!(error.to_string().contains("Invalid configuration"));
93 assert!(error.to_string().contains("bad config"));
94 }
95
96 #[test]
97 fn test_analytics_error_query() {
98 let error = AnalyticsError::Query("query failed".to_string());
99 assert!(error.to_string().contains("Query error"));
100 assert!(error.to_string().contains("query failed"));
101 }
102
103 #[test]
104 fn test_analytics_error_export() {
105 let error = AnalyticsError::Export("export failed".to_string());
106 assert!(error.to_string().contains("Export error"));
107 assert!(error.to_string().contains("export failed"));
108 }
109
110 #[test]
111 fn test_analytics_error_invalid_input() {
112 let error = AnalyticsError::InvalidInput("bad input".to_string());
113 assert!(error.to_string().contains("Invalid input"));
114 assert!(error.to_string().contains("bad input"));
115 }
116
117 #[test]
118 fn test_analytics_error_other() {
119 let error = AnalyticsError::Other("other error".to_string());
120 assert_eq!(error.to_string(), "other error");
121 }
122
123 #[test]
124 fn test_analytics_error_debug() {
125 let error = AnalyticsError::Query("test".to_string());
126 let debug = format!("{:?}", error);
127 assert!(debug.contains("Query"));
128 }
129
130 #[test]
131 fn test_analytics_error_from_serde_json() {
132 let json_error = serde_json::from_str::<i32>("not a number").unwrap_err();
133 let error: AnalyticsError = json_error.into();
134 assert!(matches!(error, AnalyticsError::Serialization(_)));
135 assert!(error.to_string().contains("Serialization error"));
136 }
137
138 #[test]
139 fn test_result_type_ok() {
140 let result: Result<i32> = Ok(42);
141 assert!(result.is_ok());
142 assert_eq!(result.unwrap(), 42);
143 }
144
145 #[test]
146 fn test_result_type_err() {
147 let result: Result<i32> = Err(AnalyticsError::Query("failed".to_string()));
148 assert!(result.is_err());
149 }
150}