baidu_netdisk_sdk/errors/
mod.rs1pub mod errno;
5
6use thiserror::Error;
7
8#[derive(Error, Debug)]
10pub enum NetDiskError {
11 #[error("HTTP request error: {status_code} - {url}")]
13 HttpError {
14 status_code: u16,
15 url: String,
16 source: Option<reqwest::Error>,
17 },
18
19 #[error("API error: {errno} - {errmsg}")]
21 ApiError { errno: i32, errmsg: String },
22
23 #[error("Authentication error: {description}")]
25 AuthError { description: String },
26
27 #[error("File already exists: {path}")]
29 FileExists { path: String },
30
31 #[error("File not found: {path}")]
33 FileNotFound { path: String },
34
35 #[error("Directory not found: {path}")]
37 DirectoryNotFound { path: String },
38
39 #[error("IO error: {0}")]
41 IoError(#[from] std::io::Error),
42
43 #[error("JSON parsing error: {0}")]
45 JsonError(#[from] serde_json::Error),
46
47 #[error("URL encoding error: {0}")]
49 UrlEncodeError(String),
50
51 #[error("URL parsing error: {0}")]
53 UrlParseError(#[from] url::ParseError),
54
55 #[error("Request timed out")]
57 Timeout,
58
59 #[error("Request cancelled")]
61 Cancelled,
62
63 #[error("Invalid parameter: {message}")]
65 InvalidParameter { message: String },
66
67 #[error("Token expired")]
69 TokenExpired,
70
71 #[error("Synchronization primitive error: {0}")]
73 SyncError(String),
74
75 #[error("Invalid header value: {0}")]
77 InvalidHeaderValue(String),
78
79 #[error("HTTP client error: {0}")]
81 ReqwestError(#[from] reqwest::Error),
82
83 #[error("MPSC send error: {0}")]
85 MpscSendError(String),
86
87 #[error("Task join error: {0}")]
89 JoinError(#[from] tokio::task::JoinError),
90
91 #[error("Unknown error: {message}")]
93 Unknown { message: String },
94}
95
96impl NetDiskError {
97 pub fn http_error(status_code: u16, url: &str) -> Self {
99 NetDiskError::HttpError {
100 status_code,
101 url: url.to_string(),
102 source: None,
103 }
104 }
105
106 pub fn http_error_with_source(status_code: u16, url: &str, source: reqwest::Error) -> Self {
108 NetDiskError::HttpError {
109 status_code,
110 url: url.to_string(),
111 source: Some(source),
112 }
113 }
114
115 pub fn api_error(errno: i32, errmsg: &str) -> Self {
117 let description = errno::get_error_description(errno).unwrap_or(errmsg);
118
119 NetDiskError::ApiError {
120 errno,
121 errmsg: description.to_string(),
122 }
123 }
124
125 pub fn api_error_raw(errno: i32, errmsg: &str) -> Self {
127 NetDiskError::ApiError {
128 errno,
129 errmsg: errmsg.to_string(),
130 }
131 }
132
133 pub fn auth_error(description: &str) -> Self {
135 NetDiskError::AuthError {
136 description: description.to_string(),
137 }
138 }
139
140 pub fn invalid_parameter(message: &str) -> Self {
142 NetDiskError::InvalidParameter {
143 message: message.to_string(),
144 }
145 }
146
147 pub fn file_exists(path: &str) -> Self {
149 NetDiskError::FileExists {
150 path: path.to_string(),
151 }
152 }
153
154 pub fn file_not_found(path: &str) -> Self {
156 NetDiskError::FileNotFound {
157 path: path.to_string(),
158 }
159 }
160
161 pub fn directory_not_found(path: &str) -> Self {
163 NetDiskError::DirectoryNotFound {
164 path: path.to_string(),
165 }
166 }
167
168 pub fn is_auth_error(&self) -> bool {
170 match self {
171 NetDiskError::ApiError { errno, .. } => errno::is_auth_error(*errno),
172 NetDiskError::TokenExpired => true,
173 NetDiskError::AuthError { .. } => true,
174 _ => false,
175 }
176 }
177
178 pub fn is_not_found_error(&self) -> bool {
180 match self {
181 NetDiskError::ApiError { errno, .. } => errno::is_not_found_error(*errno),
182 NetDiskError::FileNotFound { .. } => true,
183 NetDiskError::DirectoryNotFound { .. } => true,
184 _ => false,
185 }
186 }
187
188 pub fn is_permission_error(&self) -> bool {
190 match self {
191 NetDiskError::ApiError { errno, .. } => errno::is_permission_error(*errno),
192 _ => false,
193 }
194 }
195
196 pub fn is_quota_error(&self) -> bool {
198 match self {
199 NetDiskError::ApiError { errno, .. } => errno::is_quota_error(*errno),
200 _ => false,
201 }
202 }
203
204 pub fn is_token_expired(&self) -> bool {
206 match self {
207 NetDiskError::ApiError { errno, .. } => errno::is_token_expired(*errno),
208 NetDiskError::TokenExpired => true,
209 _ => false,
210 }
211 }
212}
213
214pub type NetDiskResult<T> = Result<T, NetDiskError>;