memscope_rs/error/
types.rs1pub use crate::core::error::MemScopeError;
7pub use crate::core::error::Result as MemScopeResult;
8
9pub struct ErrorUtils;
11
12impl ErrorUtils {
13 pub fn memory_error(operation: &str) -> MemScopeError {
15 crate::core::error::MemScopeError::memory(
16 crate::core::error::MemoryOperation::Tracking,
17 operation,
18 )
19 }
20
21 pub fn config_error(component: &str, message: &str) -> MemScopeError {
23 crate::core::error::MemScopeError::config(component, message)
24 }
25
26 pub fn system_error(message: &str) -> MemScopeError {
28 crate::core::error::MemScopeError::system(crate::core::error::SystemErrorType::Io, message)
29 }
30
31 pub fn analysis_error(analyzer: &str, message: &str) -> MemScopeError {
33 crate::core::error::MemScopeError::analysis(analyzer, message)
34 }
35
36 pub fn export_error(format: &str, message: &str) -> MemScopeError {
38 crate::core::error::MemScopeError::export(format, message)
39 }
40
41 pub fn internal_error(message: &str) -> MemScopeError {
43 crate::core::error::MemScopeError::internal(message)
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
54 fn test_memory_error_creation() {
55 let error = ErrorUtils::memory_error("test_allocation");
56 let error_string = format!("{error:?}");
57 assert!(
58 error_string.contains("Memory") || error_string.contains("memory"),
59 "Memory error should contain memory context, got: {error_string}"
60 );
61 }
62
63 #[test]
66 fn test_config_error_creation() {
67 let error = ErrorUtils::config_error("TestComponent", "invalid configuration");
68 let error_string = format!("{error:?}");
69 assert!(
70 error_string.contains("Config") || error_string.contains("config"),
71 "Config error should contain config context, got: {error_string}"
72 );
73 }
74
75 #[test]
78 fn test_system_error_creation() {
79 let error = ErrorUtils::system_error("system failure");
80 let error_string = format!("{error:?}");
81 assert!(
82 !error_string.is_empty(),
83 "System error should produce non-empty string"
84 );
85 }
86
87 #[test]
90 fn test_analysis_error_creation() {
91 let error = ErrorUtils::analysis_error("LeakDetector", "leak detection failed");
92 let error_string = format!("{error:?}");
93 assert!(
94 error_string.contains("Analysis") || error_string.contains("analysis"),
95 "Analysis error should contain analysis context, got: {error_string}"
96 );
97 }
98
99 #[test]
102 fn test_export_error_creation() {
103 let error = ErrorUtils::export_error("JSON", "serialization failed");
104 let error_string = format!("{error:?}");
105 assert!(
106 error_string.contains("Export") || error_string.contains("export"),
107 "Export error should contain export context, got: {error_string}"
108 );
109 }
110
111 #[test]
114 fn test_internal_error_creation() {
115 let error = ErrorUtils::internal_error("unexpected state");
116 let error_string = format!("{error:?}");
117 assert!(
118 error_string.contains("Internal") || error_string.contains("internal"),
119 "Internal error should contain internal context, got: {error_string}"
120 );
121 }
122
123 #[test]
126 fn test_empty_string_handling() {
127 let error1 = ErrorUtils::memory_error("");
128 let error2 = ErrorUtils::config_error("", "");
129 let error3 = ErrorUtils::system_error("");
130 let error4 = ErrorUtils::analysis_error("", "");
131 let error5 = ErrorUtils::export_error("", "");
132 let error6 = ErrorUtils::internal_error("");
133
134 assert!(!format!("{error1:?}").is_empty());
135 assert!(!format!("{error2:?}").is_empty());
136 assert!(!format!("{error3:?}").is_empty());
137 assert!(!format!("{error4:?}").is_empty());
138 assert!(!format!("{error5:?}").is_empty());
139 assert!(!format!("{error6:?}").is_empty());
140 }
141
142 #[test]
145 fn test_special_characters_handling() {
146 let error =
147 ErrorUtils::config_error("Test\nComponent", "error with \t tabs and \n newlines");
148 let error_string = format!("{error:?}");
149 assert!(
150 !error_string.is_empty(),
151 "Error with special chars should not be empty"
152 );
153 }
154
155 #[test]
158 fn test_unicode_handling() {
159 let error = ErrorUtils::internal_error("错误信息 测试 🦀");
160 let error_string = format!("{error:?}");
161 assert!(
162 error_string.contains("错误")
163 || error_string.contains("🦀")
164 || !error_string.is_empty()
165 );
166 }
167}