#[derive(Debug, thiserror::Error)]
pub enum GuardrailError {
#[error("Pipeline stage '{stage}' failed: {source}")]
StageFailed {
stage: String,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("Configuration error: {0}")]
Config(String),
#[cfg(feature = "reqwest-errors")]
#[error("Upstream request failed: {0}")]
Upstream(#[from] reqwest::Error),
#[cfg(not(feature = "reqwest-errors"))]
#[error("Upstream request failed: {0}")]
Upstream(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Regex error: {0}")]
Regex(#[from] regex::Error),
#[error("Internal error: {0}")]
Internal(String),
}
impl GuardrailError {
pub fn stage_failed(
stage: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
GuardrailError::StageFailed {
stage: stage.into(),
source: Box::new(source),
}
}
pub fn upstream(message: impl Into<String>) -> Self {
#[cfg(feature = "reqwest-errors")]
{
GuardrailError::Internal(format!("upstream: {}", message.into()))
}
#[cfg(not(feature = "reqwest-errors"))]
{
GuardrailError::Upstream(message.into())
}
}
}