use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum MessagingError {
#[error("{provider} 配置无效:{message}")]
InvalidConfig {
provider: &'static str,
message: String,
},
#[error("{provider} 消息无效:{message}")]
InvalidMessage {
provider: &'static str,
message: String,
},
#[error("{provider} 消息序列化失败:{source}")]
Serialize {
provider: &'static str,
#[source]
source: serde_json::Error,
},
#[error("请求 {provider} 失败:{source}")]
Request {
provider: &'static str,
#[source]
source: reqwest::Error,
},
#[error("{provider} {operation}失败:{source}")]
Io {
provider: &'static str,
operation: &'static str,
#[source]
source: std::io::Error,
},
#[error("{provider} 返回 HTTP {status}:{response}")]
HttpStatus {
provider: &'static str,
status: u16,
response: String,
},
#[error("{provider} 响应体超过 {max_bytes} 字节限制")]
ResponseTooLarge {
provider: &'static str,
max_bytes: usize,
},
#[error("{provider} 请求体超过 {max_bytes} 字节限制")]
RequestTooLarge {
provider: &'static str,
max_bytes: usize,
},
#[error("{provider} 请求在 {timeout:?} 内未获得并发名额")]
RequestQueueTimeout {
provider: &'static str,
timeout: std::time::Duration,
},
#[error("{provider} 返回无效响应:{response}")]
InvalidResponse {
provider: &'static str,
response: String,
#[source]
source: serde_json::Error,
},
#[error("{provider} 返回不一致的协议数据:{message};响应:{response}")]
Protocol {
provider: &'static str,
message: String,
response: String,
},
#[error("{provider} 拒绝发送({code}):{message}")]
ProviderRejected {
provider: &'static str,
code: i64,
message: String,
},
}
impl MessagingError {
#[must_use]
pub const fn provider(&self) -> &'static str {
match self {
Self::InvalidConfig { provider, .. }
| Self::InvalidMessage { provider, .. }
| Self::Serialize { provider, .. }
| Self::Request { provider, .. }
| Self::Io { provider, .. }
| Self::HttpStatus { provider, .. }
| Self::ResponseTooLarge { provider, .. }
| Self::RequestTooLarge { provider, .. }
| Self::RequestQueueTimeout { provider, .. }
| Self::InvalidResponse { provider, .. }
| Self::Protocol { provider, .. }
| Self::ProviderRejected { provider, .. } => provider,
}
}
#[must_use]
pub const fn http_status(&self) -> Option<u16> {
match self {
Self::HttpStatus { status, .. } => Some(*status),
_ => None,
}
}
#[must_use]
pub const fn provider_code(&self) -> Option<i64> {
match self {
Self::ProviderRejected { code, .. } => Some(*code),
_ => None,
}
}
#[must_use]
pub fn is_retryable(&self) -> bool {
match self {
Self::Request { source, .. } => source.is_timeout() || source.is_connect(),
Self::RequestQueueTimeout { .. } => true,
Self::HttpStatus { status, .. } => *status == 408 || *status == 429 || *status >= 500,
Self::ProviderRejected { code, .. } => *code == -1,
_ => false,
}
}
#[cfg(feature = "logging")]
pub fn to_diagnostic(&self, operation: impl Into<String>) -> crate::observability::Diagnostic {
let mut diagnostic = crate::observability::Diagnostic::new(
crate::observability::DiagnosticKind::ExternalService,
operation,
self,
)
.with_detail("provider", self.provider())
.with_detail("retryable", self.is_retryable());
if let Some(status) = self.http_status() {
diagnostic = diagnostic.with_detail("http_status", status);
}
if let Some(code) = self.provider_code() {
diagnostic = diagnostic.with_detail("provider_code", code);
}
diagnostic
}
}