Skip to main content

memscope_rs/error/
types.rs

1//! Error type definitions for MemScope
2//!
3//! Re-exports the main MemScopeError from core/error module
4//! for unified error handling across the project.
5
6pub use crate::core::error::MemScopeError;
7pub use crate::core::error::Result as MemScopeResult;
8
9/// Additional error utilities and helpers
10pub struct ErrorUtils;
11
12impl ErrorUtils {
13    /// Create a memory error with operation context
14    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    /// Create a configuration error
22    pub fn config_error(component: &str, message: &str) -> MemScopeError {
23        crate::core::error::MemScopeError::config(component, message)
24    }
25
26    /// Create a system error
27    pub fn system_error(message: &str) -> MemScopeError {
28        crate::core::error::MemScopeError::system(crate::core::error::SystemErrorType::Io, message)
29    }
30
31    /// Create an analysis error
32    pub fn analysis_error(analyzer: &str, message: &str) -> MemScopeError {
33        crate::core::error::MemScopeError::analysis(analyzer, message)
34    }
35
36    /// Create an export error
37    pub fn export_error(format: &str, message: &str) -> MemScopeError {
38        crate::core::error::MemScopeError::export(format, message)
39    }
40
41    /// Create an internal error
42    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    /// Objective: Verify ErrorUtils::memory_error creates correct error type
52    /// Invariants: Memory error should contain tracking operation context
53    #[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    /// Objective: Verify ErrorUtils::config_error creates correct error type
64    /// Invariants: Config error should contain component and message
65    #[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    /// Objective: Verify ErrorUtils::system_error creates correct error type
76    /// Invariants: System error should contain IO context
77    #[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    /// Objective: Verify ErrorUtils::analysis_error creates correct error type
88    /// Invariants: Analysis error should contain analyzer name and message
89    #[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    /// Objective: Verify ErrorUtils::export_error creates correct error type
100    /// Invariants: Export error should contain format and message
101    #[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    /// Objective: Verify ErrorUtils::internal_error creates correct error type
112    /// Invariants: Internal error should contain message
113    #[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    /// Objective: Verify empty string handling in error creation
124    /// Invariants: Empty strings should not cause panic
125    #[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    /// Objective: Verify special characters handling in error messages
143    /// Invariants: Special characters should be preserved
144    #[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    /// Objective: Verify unicode handling in error messages
156    /// Invariants: Unicode characters should be preserved
157    #[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}