use thiserror::Error;
#[derive(Error, Debug)]
pub enum SynthError {
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("Validation error: {0}")]
ValidationError(String),
#[error("Generation error: {0}")]
GenerationError(String),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Invalid data: {0}")]
InvalidData(String),
#[error("Resource exhausted: {0}")]
ResourceExhausted(String),
#[error("Memory limit exceeded: {current_mb} MB used, limit is {limit_mb} MB")]
MemoryExhausted { current_mb: usize, limit_mb: usize },
#[error("Disk space exhausted: {available_mb} MB available, need {required_mb} MB")]
DiskSpaceExhausted {
available_mb: usize,
required_mb: usize,
},
#[error("CPU overloaded: load {load:.1}% exceeds threshold {threshold:.1}%")]
CpuOverloaded { load: f64, threshold: f64 },
#[error("Resource degradation triggered: {level} - {reason}")]
DegradationTriggered { level: String, reason: String },
#[error("Channel closed unexpectedly")]
ChannelClosed,
#[error("Operation not supported: {0}")]
NotSupported(String),
}
impl SynthError {
pub fn config(msg: impl Into<String>) -> Self {
Self::ConfigError(msg.into())
}
pub fn validation(msg: impl Into<String>) -> Self {
Self::ValidationError(msg.into())
}
pub fn generation(msg: impl Into<String>) -> Self {
Self::GenerationError(msg.into())
}
pub fn invalid_data(msg: impl Into<String>) -> Self {
Self::InvalidData(msg.into())
}
pub fn resource(msg: impl Into<String>) -> Self {
Self::ResourceExhausted(msg.into())
}
pub fn not_supported(msg: impl Into<String>) -> Self {
Self::NotSupported(msg.into())
}
pub fn memory_exhausted(current_mb: usize, limit_mb: usize) -> Self {
Self::MemoryExhausted {
current_mb,
limit_mb,
}
}
pub fn disk_exhausted(available_mb: usize, required_mb: usize) -> Self {
Self::DiskSpaceExhausted {
available_mb,
required_mb,
}
}
pub fn cpu_overloaded(load: f64, threshold: f64) -> Self {
Self::CpuOverloaded { load, threshold }
}
pub fn degradation(level: impl Into<String>, reason: impl Into<String>) -> Self {
Self::DegradationTriggered {
level: level.into(),
reason: reason.into(),
}
}
pub fn is_recoverable(&self) -> bool {
matches!(self, Self::DegradationTriggered { .. })
}
pub fn is_resource_error(&self) -> bool {
matches!(
self,
Self::ResourceExhausted(_)
| Self::MemoryExhausted { .. }
| Self::DiskSpaceExhausted { .. }
| Self::CpuOverloaded { .. }
| Self::DegradationTriggered { .. }
)
}
}
pub type SynthResult<T> = Result<T, SynthError>;