use std::error::Error as StdError;
use std::time::Duration;
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("未授权:请重新登录")]
Unauthorized,
#[error("请求过于频繁,请稍后再试")]
RateLimited { retry_after: Option<Duration> },
#[error("资源不存在: {resource}")]
NotFound { resource: String },
#[error("业务错误[{code}]: {msg}")]
Business { code: i64, msg: String },
#[error("传输错误: {0}")]
Transport(#[from] reqwest::Error),
#[error("解析错误: {0}")]
Decode(String),
#[error("WebSocket 错误: {0}")]
WebSocket(String),
#[error("Request error: {0}")]
Request(Box<dyn StdError + Send + Sync>),
#[error("{0}")]
Api(String),
#[error("Parse error: {0}")]
Parse(String),
}
impl Error {
pub fn is_retryable(&self) -> bool {
matches!(
self,
Error::Transport(_) | Error::RateLimited { .. } | Error::Request(_)
)
}
pub fn business(code: i64, msg: impl Into<String>) -> Self {
Error::Business {
code,
msg: msg.into(),
}
}
pub fn decode(msg: impl Into<String>) -> Self {
Error::Decode(msg.into())
}
}