1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum SynthError {
8 #[error("Configuration error: {0}")]
10 ConfigError(String),
11
12 #[error("Validation error: {0}")]
14 ValidationError(String),
15
16 #[error("Generation error: {0}")]
18 GenerationError(String),
19
20 #[error("IO error: {0}")]
22 IoError(#[from] std::io::Error),
23
24 #[error("Serialization error: {0}")]
26 SerializationError(String),
27
28 #[error("Invalid data: {0}")]
30 InvalidData(String),
31
32 #[error("Resource exhausted: {0}")]
34 ResourceExhausted(String),
35
36 #[error("Memory limit exceeded: {current_mb} MB used, limit is {limit_mb} MB")]
38 MemoryExhausted { current_mb: usize, limit_mb: usize },
39
40 #[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 #[error("CPU overloaded: load {load:.1}% exceeds threshold {threshold:.1}%")]
49 CpuOverloaded { load: f64, threshold: f64 },
50
51 #[error("Resource degradation triggered: {level} - {reason}")]
53 DegradationTriggered { level: String, reason: String },
54
55 #[error("Channel closed unexpectedly")]
57 ChannelClosed,
58
59 #[error("Operation not supported: {0}")]
61 NotSupported(String),
62}
63
64impl SynthError {
65 pub fn config(msg: impl Into<String>) -> Self {
67 Self::ConfigError(msg.into())
68 }
69
70 pub fn validation(msg: impl Into<String>) -> Self {
72 Self::ValidationError(msg.into())
73 }
74
75 pub fn generation(msg: impl Into<String>) -> Self {
77 Self::GenerationError(msg.into())
78 }
79
80 pub fn invalid_data(msg: impl Into<String>) -> Self {
82 Self::InvalidData(msg.into())
83 }
84
85 pub fn resource(msg: impl Into<String>) -> Self {
87 Self::ResourceExhausted(msg.into())
88 }
89
90 pub fn not_supported(msg: impl Into<String>) -> Self {
92 Self::NotSupported(msg.into())
93 }
94
95 pub fn memory_exhausted(current_mb: usize, limit_mb: usize) -> Self {
97 Self::MemoryExhausted {
98 current_mb,
99 limit_mb,
100 }
101 }
102
103 pub fn disk_exhausted(available_mb: usize, required_mb: usize) -> Self {
105 Self::DiskSpaceExhausted {
106 available_mb,
107 required_mb,
108 }
109 }
110
111 pub fn cpu_overloaded(load: f64, threshold: f64) -> Self {
113 Self::CpuOverloaded { load, threshold }
114 }
115
116 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 pub fn is_recoverable(&self) -> bool {
126 matches!(self, Self::DegradationTriggered { .. })
127 }
128
129 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
142pub type SynthResult<T> = Result<T, SynthError>;