Skip to main content

baidu_netdisk_sdk/errors/
mod.rs

1/// Error handling module for Baidu NetDisk SDK
2///
3/// This module provides error types and utilities for handling API errors.
4pub mod errno;
5
6use thiserror::Error;
7
8/// SDK error types
9#[derive(Error, Debug)]
10pub enum NetDiskError {
11    /// HTTP request error
12    #[error("HTTP request error: {status_code} - {url}")]
13    HttpError {
14        status_code: u16,
15        url: String,
16        source: Option<reqwest::Error>,
17    },
18
19    /// API business error
20    #[error("API error: {errno} - {errmsg}")]
21    ApiError { errno: i32, errmsg: String },
22
23    /// Authentication error
24    #[error("Authentication error: {description}")]
25    AuthError { description: String },
26
27    /// File already exists
28    #[error("File already exists: {path}")]
29    FileExists { path: String },
30
31    /// File not found
32    #[error("File not found: {path}")]
33    FileNotFound { path: String },
34
35    /// Directory not found
36    #[error("Directory not found: {path}")]
37    DirectoryNotFound { path: String },
38
39    /// IO error
40    #[error("IO error: {0}")]
41    IoError(#[from] std::io::Error),
42
43    /// JSON parsing error
44    #[error("JSON parsing error: {0}")]
45    JsonError(#[from] serde_json::Error),
46
47    /// URL encoding error
48    #[error("URL encoding error: {0}")]
49    UrlEncodeError(String),
50
51    /// URL parsing error
52    #[error("URL parsing error: {0}")]
53    UrlParseError(#[from] url::ParseError),
54
55    /// Request timeout
56    #[error("Request timed out")]
57    Timeout,
58
59    /// Request cancelled
60    #[error("Request cancelled")]
61    Cancelled,
62
63    /// Invalid parameter
64    #[error("Invalid parameter: {message}")]
65    InvalidParameter { message: String },
66
67    /// Token expired
68    #[error("Token expired")]
69    TokenExpired,
70
71    /// Synchronization primitive error
72    #[error("Synchronization primitive error: {0}")]
73    SyncError(String),
74
75    /// Invalid header value
76    #[error("Invalid header value: {0}")]
77    InvalidHeaderValue(String),
78
79    /// Reqwest HTTP client error
80    #[error("HTTP client error: {0}")]
81    ReqwestError(#[from] reqwest::Error),
82
83    /// MPSC send error
84    #[error("MPSC send error: {0}")]
85    MpscSendError(String),
86
87    /// Join error (task join error)
88    #[error("Task join error: {0}")]
89    JoinError(#[from] tokio::task::JoinError),
90
91    /// Unknown error
92    #[error("Unknown error: {message}")]
93    Unknown { message: String },
94}
95
96impl NetDiskError {
97    /// Create HTTP error
98    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    /// Create HTTP error with source
107    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    /// Create API error with standard error description
116    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    /// Create API error with raw error message
126    pub fn api_error_raw(errno: i32, errmsg: &str) -> Self {
127        NetDiskError::ApiError {
128            errno,
129            errmsg: errmsg.to_string(),
130        }
131    }
132
133    /// Create authentication error
134    pub fn auth_error(description: &str) -> Self {
135        NetDiskError::AuthError {
136            description: description.to_string(),
137        }
138    }
139
140    /// Create invalid parameter error
141    pub fn invalid_parameter(message: &str) -> Self {
142        NetDiskError::InvalidParameter {
143            message: message.to_string(),
144        }
145    }
146
147    /// Create file exists error
148    pub fn file_exists(path: &str) -> Self {
149        NetDiskError::FileExists {
150            path: path.to_string(),
151        }
152    }
153
154    /// Create file not found error
155    pub fn file_not_found(path: &str) -> Self {
156        NetDiskError::FileNotFound {
157            path: path.to_string(),
158        }
159    }
160
161    /// Create directory not found error
162    pub fn directory_not_found(path: &str) -> Self {
163        NetDiskError::DirectoryNotFound {
164            path: path.to_string(),
165        }
166    }
167
168    /// Check if error is authentication related
169    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    /// Check if error indicates not found
179    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    /// Check if error is permission related
189    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    /// Check if error is quota related
197    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    /// Check if error indicates token expired
205    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
214/// SDK result type
215pub type NetDiskResult<T> = Result<T, NetDiskError>;