datasynth_core/
error.rs

1//! Error types for the synthetic data generation system.
2
3use thiserror::Error;
4
5/// Main error type for synthetic data operations.
6#[derive(Error, Debug)]
7pub enum SynthError {
8    /// Configuration error
9    #[error("Configuration error: {0}")]
10    ConfigError(String),
11
12    /// Validation error
13    #[error("Validation error: {0}")]
14    ValidationError(String),
15
16    /// Generation error
17    #[error("Generation error: {0}")]
18    GenerationError(String),
19
20    /// IO error
21    #[error("IO error: {0}")]
22    IoError(#[from] std::io::Error),
23
24    /// Serialization error
25    #[error("Serialization error: {0}")]
26    SerializationError(String),
27
28    /// Invalid data error
29    #[error("Invalid data: {0}")]
30    InvalidData(String),
31
32    /// Resource exhausted
33    #[error("Resource exhausted: {0}")]
34    ResourceExhausted(String),
35
36    /// Memory limit exceeded
37    #[error("Memory limit exceeded: {current_mb} MB used, limit is {limit_mb} MB")]
38    MemoryExhausted { current_mb: usize, limit_mb: usize },
39
40    /// Disk space exhausted
41    #[error("Disk space exhausted: {available_mb} MB available, need {required_mb} MB")]
42    DiskSpaceExhausted {
43        available_mb: usize,
44        required_mb: usize,
45    },
46
47    /// CPU overload
48    #[error("CPU overloaded: load {load:.1}% exceeds threshold {threshold:.1}%")]
49    CpuOverloaded { load: f64, threshold: f64 },
50
51    /// Resource degradation triggered
52    #[error("Resource degradation triggered: {level} - {reason}")]
53    DegradationTriggered { level: String, reason: String },
54
55    /// Channel closed (for streaming)
56    #[error("Channel closed unexpectedly")]
57    ChannelClosed,
58
59    /// Not supported
60    #[error("Operation not supported: {0}")]
61    NotSupported(String),
62}
63
64impl SynthError {
65    /// Create a configuration error.
66    pub fn config(msg: impl Into<String>) -> Self {
67        Self::ConfigError(msg.into())
68    }
69
70    /// Create a validation error.
71    pub fn validation(msg: impl Into<String>) -> Self {
72        Self::ValidationError(msg.into())
73    }
74
75    /// Create a generation error.
76    pub fn generation(msg: impl Into<String>) -> Self {
77        Self::GenerationError(msg.into())
78    }
79
80    /// Create an invalid data error.
81    pub fn invalid_data(msg: impl Into<String>) -> Self {
82        Self::InvalidData(msg.into())
83    }
84
85    /// Create a resource exhausted error.
86    pub fn resource(msg: impl Into<String>) -> Self {
87        Self::ResourceExhausted(msg.into())
88    }
89
90    /// Create a not supported error.
91    pub fn not_supported(msg: impl Into<String>) -> Self {
92        Self::NotSupported(msg.into())
93    }
94
95    /// Create a memory exhausted error.
96    pub fn memory_exhausted(current_mb: usize, limit_mb: usize) -> Self {
97        Self::MemoryExhausted {
98            current_mb,
99            limit_mb,
100        }
101    }
102
103    /// Create a disk space exhausted error.
104    pub fn disk_exhausted(available_mb: usize, required_mb: usize) -> Self {
105        Self::DiskSpaceExhausted {
106            available_mb,
107            required_mb,
108        }
109    }
110
111    /// Create a CPU overloaded error.
112    pub fn cpu_overloaded(load: f64, threshold: f64) -> Self {
113        Self::CpuOverloaded { load, threshold }
114    }
115
116    /// Create a degradation triggered error.
117    pub fn degradation(level: impl Into<String>, reason: impl Into<String>) -> Self {
118        Self::DegradationTriggered {
119            level: level.into(),
120            reason: reason.into(),
121        }
122    }
123
124    /// Check if this error is recoverable (degradation or soft limits).
125    pub fn is_recoverable(&self) -> bool {
126        matches!(self, Self::DegradationTriggered { .. })
127    }
128
129    /// Check if this error is a resource-related error.
130    pub fn is_resource_error(&self) -> bool {
131        matches!(
132            self,
133            Self::ResourceExhausted(_)
134                | Self::MemoryExhausted { .. }
135                | Self::DiskSpaceExhausted { .. }
136                | Self::CpuOverloaded { .. }
137                | Self::DegradationTriggered { .. }
138        )
139    }
140}
141
142/// Result type alias for synthetic data operations.
143pub type SynthResult<T> = Result<T, SynthError>;