1pub type Result<T> = std::result::Result<T, TqError>;
7
8#[derive(Debug, thiserror::Error)]
10pub enum TqError {
11 #[error("权限不足: {0}")]
13 PermissionDenied(String),
14
15 #[error("WebSocket 连接错误: {0}")]
17 WebSocketError(String),
18
19 #[error("认证失败: {0}")]
21 AuthenticationError(String),
22
23 #[error("数据解析错误: {0}")]
25 ParseError(String),
26
27 #[error("网络请求错误: {0}")]
29 NetworkError(String),
30
31 #[error("配置错误: {0}")]
33 ConfigError(String),
34
35 #[error("无效的合约代码: {0}")]
37 InvalidSymbol(String),
38
39 #[error("无效的参数: {0}")]
41 InvalidParameter(String),
42
43 #[error("交易错误: {0}")]
45 TradeError(String),
46
47 #[error("订单不存在: {0}")]
49 OrderNotFound(String),
50
51 #[error("账户未登录")]
53 NotLoggedIn,
54
55 #[error("数据未找到: {0}")]
57 DataNotFound(String),
58
59 #[error("内部错误: {0}")]
61 InternalError(String),
62
63 #[error("操作超时")]
65 Timeout,
66
67 #[error("IO 错误: {0}")]
69 IoError(String),
70
71 #[error("JSON 错误: {0}")]
73 JsonError(String),
74
75 #[error("JWT 错误: {0}")]
77 JwtError(String),
78
79 #[error("错误: {0}")]
81 Other(String),
82}
83
84impl 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
116impl TqError {
118 pub fn permission_denied_futures() -> Self {
120 TqError::PermissionDenied(
121 "您的账户不支持查看期货行情数据,需要购买后才能使用。\
122 升级网址: https://www.shinnytech.com/tqsdk-buy/"
123 .to_string(),
124 )
125 }
126
127 pub fn permission_denied_stocks() -> Self {
129 TqError::PermissionDenied(
130 "您的账户不支持查看股票行情数据,需要购买后才能使用。\
131 升级网址: https://www.shinnytech.com/tqsdk-buy/"
132 .to_string(),
133 )
134 }
135
136 pub fn permission_denied_history() -> Self {
138 TqError::PermissionDenied(
139 "数据获取方式仅限专业版用户使用,如需购买专业版或者申请试用,\
140 请访问 https://www.shinnytech.com/tqsdk-buy/"
141 .to_string(),
142 )
143 }
144
145 pub fn invalid_left_kline_id() -> Self {
147 TqError::InvalidParameter("left_kline_id 必须大于 0".to_string())
148 }
149
150 pub fn invalid_focus_position() -> Self {
152 TqError::InvalidParameter(
153 "使用 focus_datetime 时必须提供 focus_position,且 focus_position 必须 >= 0"
154 .to_string(),
155 )
156 }
157}