use thiserror::Error;
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[error("连接 `{connector}` 未启用,无法执行操作")]
pub struct ConnectionUnavailable {
connector: String,
}
impl ConnectionUnavailable {
pub(crate) fn new(connector: impl Into<String>) -> Self {
Self {
connector: connector.into(),
}
}
#[must_use]
pub fn connector(&self) -> &str {
&self.connector
}
}
#[derive(Debug, Error)]
pub enum ManagedConnectionError<E>
where
E: std::error::Error + 'static,
{
#[error(transparent)]
Unavailable(#[from] ConnectionUnavailable),
#[error(transparent)]
Connector(E),
}
#[cfg(any(
feature = "connection-mysql",
feature = "connection-postgres",
feature = "connection-redis",
feature = "connection-kafka",
feature = "connection-etcd",
feature = "connection-mqtt",
feature = "connection-elasticsearch"
))]
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ConnectionError {
#[cfg(any(
feature = "connection-kafka",
feature = "connection-etcd",
feature = "connection-mqtt",
feature = "connection-redis",
feature = "connection-elasticsearch"
))]
#[error("{connector} 配置无效:{message}")]
InvalidConfig {
connector: &'static str,
message: &'static str,
},
#[cfg(any(feature = "connection-mysql", feature = "connection-postgres"))]
#[error("数据库连接池配置无效:{0}")]
InvalidPoolOptions(String),
#[cfg(any(feature = "connection-mysql", feature = "connection-postgres"))]
#[error("数据库连接失败:{0}")]
Sqlx(#[from] sqlx::Error),
#[cfg(feature = "connection-redis")]
#[error("Redis 连接失败:{0}")]
Redis(#[from] redis::RedisError),
#[cfg(feature = "connection-redis")]
#[error("Redis 健康检查返回异常:{0}")]
UnexpectedRedisPing(String),
#[cfg(feature = "connection-kafka")]
#[error("Kafka 连接失败:{0}")]
Kafka(#[from] rdkafka::error::KafkaError),
#[cfg(feature = "connection-etcd")]
#[error("etcd 连接失败:{0}")]
Etcd(#[from] etcd_client::Error),
#[cfg(feature = "connection-mqtt")]
#[error("MQTT 客户端操作失败:{0}")]
MqttClient(#[from] rumqttc::ClientError),
#[cfg(feature = "connection-mqtt")]
#[error("MQTT 配置无效:{0}")]
MqttOptions(#[from] rumqttc::OptionError),
#[cfg(feature = "connection-mqtt")]
#[error("MQTT 连接失败:{0}")]
MqttConnection(#[source] Box<rumqttc::ConnectionError>),
#[cfg(feature = "connection-mqtt")]
#[error("MQTT 当前未连接到代理服务器")]
MqttDisconnected,
#[cfg(feature = "connection-mqtt")]
#[error("MQTT 在 {0:?} 内未完成首次连接")]
MqttConnectTimeout(std::time::Duration),
#[cfg(feature = "connection-mqtt")]
#[error("MQTT 在 {0:?} 内未完成关闭")]
MqttShutdownTimeout(std::time::Duration),
#[cfg(any(feature = "connection-kafka", feature = "connection-mqtt"))]
#[error("连接后台任务执行失败:{0}")]
BackgroundTask(#[from] tokio::task::JoinError),
#[cfg(feature = "connection-elasticsearch")]
#[error("Elasticsearch 连接失败:{0}")]
Elasticsearch(#[from] elasticsearch::Error),
#[cfg(feature = "connection-elasticsearch")]
#[error("Elasticsearch 健康检查返回 HTTP {0}")]
ElasticsearchHealth(u16),
}
#[cfg(feature = "connection-mqtt")]
impl From<rumqttc::ConnectionError> for ConnectionError {
fn from(error: rumqttc::ConnectionError) -> Self {
Self::MqttConnection(Box::new(error))
}
}