use miette::Diagnostic;
use std::path::PathBuf;
use thiserror::Error;
#[derive(Error, Debug, Diagnostic)]
pub enum Error {
#[error("Configuration error: {message}")]
#[diagnostic(code(cuenv_hooks::config::invalid))]
Configuration {
message: String,
},
#[error("I/O error during {operation}: {source}")]
#[diagnostic(code(cuenv_hooks::io::error))]
Io {
#[source]
source: std::io::Error,
path: Option<Box<std::path::Path>>,
operation: String,
},
#[error("Operation timed out after {seconds} seconds")]
#[diagnostic(code(cuenv_hooks::timeout))]
Timeout {
seconds: u64,
},
#[error("Execution state not found for instance: {instance_id}")]
#[diagnostic(code(cuenv_hooks::state::not_found))]
StateNotFound {
instance_id: String,
},
#[error("Serialization error: {message}")]
#[diagnostic(code(cuenv_hooks::serialization))]
Serialization {
message: String,
},
#[error("Process execution failed: {message}")]
#[diagnostic(code(cuenv_hooks::process))]
Process {
message: String,
},
}
impl Error {
pub fn configuration(message: impl Into<String>) -> Self {
Self::Configuration {
message: message.into(),
}
}
pub fn io(source: std::io::Error, path: Option<PathBuf>, operation: impl Into<String>) -> Self {
Self::Io {
source,
path: path.map(|p| p.into_boxed_path()),
operation: operation.into(),
}
}
pub fn state_not_found(instance_id: impl Into<String>) -> Self {
Self::StateNotFound {
instance_id: instance_id.into(),
}
}
pub fn serialization(message: impl Into<String>) -> Self {
Self::Serialization {
message: message.into(),
}
}
pub fn process(message: impl Into<String>) -> Self {
Self::Process {
message: message.into(),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;