use thiserror::Error;
#[derive(Debug, Error)]
pub enum NasaRedisError {
#[error(transparent)]
ReadBudget(#[from] nabudget::BudgetError),
#[error("partition start rollback not converged: {cause}; remaining: {remaining:?}")]
StartRollbackNotConverged {
cause: String,
remaining: Box<crate::partition::PartitionShutdownReport>,
},
#[error("partition publish outcome unknown: {0}")]
PublishOutcomeUnknown(String),
#[error("配置错误: {0}")]
Config(String),
#[error("协议标记校验失败: {0}")]
ProtocolMarker(String),
#[error("redis 错误: {0}")]
Redis(#[from] redis::RedisError),
#[error("{0}")]
Idempotent(#[from] crate::idempotent::IdempotentCounterError),
#[error("幂等计数协议错误: {0}")]
IdempotentProtocol(String),
#[error("job 协议错误: {0}")]
JobProtocol(String),
#[cfg(feature = "job")]
#[error(transparent)]
Job(#[from] crate::job::JobError),
#[error("job 执行已停止: {0}")]
JobExecutionStopped(String),
#[error("probe failed during {stage} on {endpoint}")]
ConnectProbe {
stage: &'static str,
endpoint: String,
#[source]
source: redis::RedisError,
},
#[error("响应解析错误: {0}")]
Parsing(#[from] redis::ParsingError),
#[error("执行结果不确定(可能已执行): {0}")]
ExecutionUnknown(String),
#[error("命令未发送(会话未 execute 即丢弃): {0}")]
NotExecuted(String),
#[error("pipeline 会话超限: {0}")]
SessionLimit(String),
#[error("仍有 {remaining} 个重入 permit,锁未释放")]
StillReentered {
remaining: u32,
},
#[error("未持有锁: {0}")]
LockNotHeld(String),
#[error("等锁超时: {0}")]
LockTimeout(String),
#[error("编解码失败: {0}")]
Codec(String),
#[error("发布结果不确定(PublishOutcomeIndeterminate): {0}")]
PublishIndeterminate(String),
#[error("操作存疑(OperationInDoubt): {0}")]
OperationInDoubt(String),
#[error("owner/任期已变更(OwnershipChanged): {0}")]
OwnershipChanged(String),
#[error("operation 冲突(OperationConflict): {0}")]
OperationConflict(String),
}
impl NasaRedisError {
pub fn is_infra_retryable(&self) -> bool {
match self {
NasaRedisError::Redis(e) => redis_is_transient(e),
NasaRedisError::ExecutionUnknown(_) => true,
NasaRedisError::OwnershipChanged(_) => true,
NasaRedisError::OperationConflict(_) => false,
_ => false,
}
}
}
pub(crate) fn redis_is_transient(e: &redis::RedisError) -> bool {
if e.is_io_error() || e.is_timeout() || e.is_connection_dropped() {
return true;
}
matches!(
e.code(),
Some("TRYAGAIN") | Some("LOADING") | Some("CLUSTERDOWN") | Some("MASTERDOWN")
)
}
pub type Result<T> = std::result::Result<T, NasaRedisError>;