use std::error::Error;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, ChrononError>;
#[derive(Debug, Error)]
pub enum ChrononError {
#[error("script not found: {0}")]
ScriptNotFound(String),
#[error("job not found: {0}")]
JobNotFound(String),
#[error("run not found: {0}")]
RunNotFound(String),
#[error("invalid cron expression: {0}")]
InvalidCron(String),
#[error("invalid timezone: {0}")]
InvalidTimezone(String),
#[error("parameter error: {0}")]
ParamError(String),
#[error("script mismatch for job '{job_name}': expected '{expected}', got '{actual}'")]
ScriptMismatch {
expected: String,
actual: String,
job_name: String,
},
#[error("storage error: {message}")]
StorageError {
message: String,
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
},
#[error("identity error: {0}")]
Identity(String),
#[error("internal error: {0}")]
Internal(String),
#[error("internal error: {message}")]
InternalSource {
message: String,
#[source]
source: Box<dyn Error + Send + Sync>,
},
}
impl ChrononError {
pub fn storage(message: impl Into<String>) -> Self {
Self::StorageError {
message: message.into(),
source: None,
}
}
pub fn storage_source(
message: impl Into<String>,
source: impl Error + Send + Sync + 'static,
) -> Self {
Self::StorageError {
message: message.into(),
source: Some(Box::new(source)),
}
}
pub fn internal_source(
message: impl Into<String>,
source: impl Error + Send + Sync + 'static,
) -> Self {
Self::InternalSource {
message: message.into(),
source: Box::new(source),
}
}
}
impl From<serde_json::Error> for ChrononError {
fn from(err: serde_json::Error) -> Self {
Self::ParamError(err.to_string())
}
}