tqsdk_rs/
errors.rs

1//! 错误类型定义
2//!
3//! 使用 thiserror 定义所有错误类型
4
5/// TQSDK Result 类型
6pub type Result<T> = std::result::Result<T, TqError>;
7
8/// TQSDK 错误类型
9#[derive(Debug, thiserror::Error)]
10pub enum TqError {
11    /// 权限不足错误
12    #[error("权限不足: {0}")]
13    PermissionDenied(String),
14
15    /// WebSocket 连接错误
16    #[error("WebSocket 连接错误: {0}")]
17    WebSocketError(String),
18
19    /// 认证失败
20    #[error("认证失败: {0}")]
21    AuthenticationError(String),
22
23    /// 数据解析错误
24    #[error("数据解析错误: {0}")]
25    ParseError(String),
26
27    /// 网络请求错误
28    #[error("网络请求错误: {0}")]
29    NetworkError(String),
30
31    /// 配置错误
32    #[error("配置错误: {0}")]
33    ConfigError(String),
34
35    /// 无效的合约代码
36    #[error("无效的合约代码: {0}")]
37    InvalidSymbol(String),
38
39    /// 无效的参数
40    #[error("无效的参数: {0}")]
41    InvalidParameter(String),
42
43    /// 交易错误
44    #[error("交易错误: {0}")]
45    TradeError(String),
46
47    /// 订单不存在
48    #[error("订单不存在: {0}")]
49    OrderNotFound(String),
50
51    /// 账户未登录
52    #[error("账户未登录")]
53    NotLoggedIn,
54
55    /// 数据未找到
56    #[error("数据未找到: {0}")]
57    DataNotFound(String),
58
59    /// 内部错误
60    #[error("内部错误: {0}")]
61    InternalError(String),
62
63    /// 超时错误
64    #[error("操作超时")]
65    Timeout,
66
67    /// IO 错误
68    #[error("IO 错误: {0}")]
69    IoError(String),
70
71    /// JSON 错误
72    #[error("JSON 错误: {0}")]
73    JsonError(String),
74
75    /// JWT 错误
76    #[error("JWT 错误: {0}")]
77    JwtError(String),
78
79    /// 其他错误
80    #[error("错误: {0}")]
81    Other(String),
82}
83
84// 实现 From trait 用于错误转换
85
86impl From<std::io::Error> for TqError {
87    fn from(err: std::io::Error) -> Self {
88        TqError::IoError(err.to_string())
89    }
90}
91
92impl From<serde_json::Error> for TqError {
93    fn from(err: serde_json::Error) -> Self {
94        TqError::JsonError(err.to_string())
95    }
96}
97
98impl From<reqwest::Error> for TqError {
99    fn from(err: reqwest::Error) -> Self {
100        TqError::NetworkError(err.to_string())
101    }
102}
103
104impl From<jsonwebtoken::errors::Error> for TqError {
105    fn from(err: jsonwebtoken::errors::Error) -> Self {
106        TqError::JwtError(err.to_string())
107    }
108}
109
110impl From<url::ParseError> for TqError {
111    fn from(err: url::ParseError) -> Self {
112        TqError::InvalidParameter(err.to_string())
113    }
114}
115
116// 预定义的错误常量
117impl TqError {
118    /// 权限不足 - 期货行情
119    pub fn permission_denied_futures() -> Self {
120        TqError::PermissionDenied(
121            "您的账户不支持查看期货行情数据,需要购买后才能使用。\
122             升级网址: https://www.shinnytech.com/tqsdk-buy/"
123                .to_string(),
124        )
125    }
126
127    /// 权限不足 - 股票行情
128    pub fn permission_denied_stocks() -> Self {
129        TqError::PermissionDenied(
130            "您的账户不支持查看股票行情数据,需要购买后才能使用。\
131             升级网址: https://www.shinnytech.com/tqsdk-buy/"
132                .to_string(),
133        )
134    }
135
136    /// 权限不足 - 历史数据
137    pub fn permission_denied_history() -> Self {
138        TqError::PermissionDenied(
139            "数据获取方式仅限专业版用户使用,如需购买专业版或者申请试用,\
140             请访问 https://www.shinnytech.com/tqsdk-buy/"
141                .to_string(),
142        )
143    }
144
145    /// 无效的 left_kline_id
146    pub fn invalid_left_kline_id() -> Self {
147        TqError::InvalidParameter("left_kline_id 必须大于 0".to_string())
148    }
149
150    /// 无效的 focus_position
151    pub fn invalid_focus_position() -> Self {
152        TqError::InvalidParameter(
153            "使用 focus_datetime 时必须提供 focus_position,且 focus_position 必须 >= 0"
154                .to_string(),
155        )
156    }
157}