#[derive(Debug, thiserror::Error)]
pub enum CascadeError {
#[error("{0}")]
Git(#[from] git2::Error),
#[error("{0}")]
Config(String),
#[error("{0}")]
Branch(String),
#[error("{0}")]
Network(String),
#[error("{0}")]
Auth(String),
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
Json(#[from] serde_json::Error),
#[error("{0}")]
Http(#[from] reqwest::Error),
#[error("{0}")]
Url(#[from] url::ParseError),
#[error("{0}")]
Conflict(String),
#[error("{0}")]
Corruption(String),
#[error("{0}")]
Rebase(String),
#[error("{0}")]
MissingDependency(String),
#[error("{0}")]
RateLimit(String),
#[error("{0}")]
Validation(String),
}
impl CascadeError {
pub fn config<S: Into<String>>(msg: S) -> Self {
CascadeError::Config(msg.into())
}
pub fn branch<S: Into<String>>(msg: S) -> Self {
CascadeError::Branch(msg.into())
}
pub fn auth<S: Into<String>>(msg: S) -> Self {
CascadeError::Auth(msg.into())
}
pub fn validation<S: Into<String>>(msg: S) -> Self {
CascadeError::Validation(msg.into())
}
pub fn parse<S: Into<String>>(msg: S) -> Self {
CascadeError::Validation(msg.into())
}
pub fn not_initialized<S: Into<String>>(msg: S) -> Self {
CascadeError::config(msg.into())
}
pub fn invalid_operation<S: Into<String>>(msg: S) -> Self {
CascadeError::Validation(msg.into())
}
pub fn conflict_resolution<S: Into<String>>(file: S, reason: S) -> Self {
CascadeError::Conflict(format!("{}: {}", file.into(), reason.into()))
}
pub fn bitbucket_api(status: u16, message: String) -> Self {
CascadeError::Conflict(format!("Bitbucket API error: {status} - {message}"))
}
pub fn bitbucket<S: Into<String>>(msg: S) -> Self {
CascadeError::Conflict(msg.into())
}
pub fn is_lock_error(&self) -> bool {
match self {
CascadeError::Git(e) => crate::utils::git_lock::is_lock_error(e),
CascadeError::Branch(msg) | CascadeError::Config(msg) | CascadeError::Rebase(msg) => {
msg.contains("index is locked") || msg.contains("index.lock")
}
_ => false,
}
}
}
pub type Result<T> = std::result::Result<T, CascadeError>;