cloud_disk_sync/error/
mod.rs

1use reqwest::Error as ReqwestError;
2use rusqlite::Error as RusqliteError;
3use serde_json::Error as SerdeJsonError;
4use std::io::Error as IoError;
5use std::path::PathBuf;
6use thiserror::Error;
7
8#[derive(Error, Debug)]
9pub enum SyncError {
10    #[error("Configuration error: {0}")]
11    Config(#[from] ConfigError),
12
13    #[error("Provider error: {0}")]
14    Provider(#[from] ProviderError),
15
16    #[error("Encryption error: {0}")]
17    Encryption(#[from] EncryptionError),
18
19    #[error("Storage error: {0}")]
20    Storage(#[from] StorageError),
21
22    #[error("IO error: {0}")]
23    Io(#[from] IoError),
24
25    #[error("Network error: {0}")]
26    Network(#[from] ReqwestError),
27
28    #[error("Database error: {0}")]
29    Database(#[from] RusqliteError),
30
31    #[error("Serialization error: {0}")]
32    Serialization(#[from] SerdeJsonError),
33
34    #[error("Validation error: {0}")]
35    Validation(String),
36
37    #[error("Timeout error: {0}")]
38    Timeout(String),
39
40    #[error("Rate limit exceeded: {0}")]
41    RateLimitExceeded(String),
42
43    #[error("Authentication failed: {0}")]
44    AuthenticationFailed(String),
45
46    #[error("Permission denied: {0}")]
47    PermissionDenied(String),
48
49    #[error("File not found: {0}")]
50    FileNotFound(String),
51
52    #[error("Resource exhausted: {0}")]
53    ResourceExhausted(String),
54
55    #[error("Conflict detected: {0}")]
56    Conflict(String),
57
58    #[error("Integrity check failed: {0}")]
59    IntegrityCheckFailed(String),
60
61    #[error("Retry limit exceeded: {0}")]
62    RetryLimitExceeded(String),
63
64    #[error("Operation canceled: ")]
65    OperationCanceled,
66
67    #[error("Unsupported feature: {0}")]
68    Unsupported(String),
69
70    #[error("Unknown error: {0}")]
71    Unknown(String),
72}
73
74#[derive(Error, Debug)]
75pub enum ConfigError {
76    #[error("Configuration file not found: {0}")]
77    FileNotFound(PathBuf),
78
79    #[error("Failed to read config file: {0}")]
80    ReadFailed(#[source] IoError),
81
82    #[error("Failed to parse config: {0}")]
83    ParseFailed(String),
84
85    #[error("Missing required field: {0}")]
86    MissingField(String),
87
88    #[error("Invalid configuration: {0}")]
89    Invalid(String),
90
91    #[error("Configuration directory not found")]
92    NoConfigDir,
93}
94
95#[derive(Error, Debug)]
96pub enum ProviderError {
97    #[error("Provider not found: {0}")]
98    NotFound(String),
99
100    #[error("Provider not supported: {0}")]
101    NotSupported(String),
102
103    #[error("Missing credentials for provider: {0}")]
104    MissingCredentials(String),
105
106    #[error("Invalid credentials: {0}")]
107    InvalidCredentials(String),
108
109    #[error("Provider quota exceeded: {0}")]
110    QuotaExceeded(String),
111
112    #[error("Provider rate limited: {0}")]
113    RateLimited(String),
114
115    #[error("Provider authentication failed: {0}")]
116    AuthFailed(String),
117
118    #[error("Provider connection failed: {0}")]
119    ConnectionFailed(String),
120
121    #[error("Provider API error: {0}")]
122    ApiError(String),
123
124    #[error("Provider timeout: {0}")]
125    Timeout(String),
126
127    #[error("Provider file not found: {0}")]
128    FileNotFound(String),
129
130    #[error("Provider permission denied: {0}")]
131    PermissionDenied(String),
132}
133
134#[derive(Error, Debug)]
135pub enum EncryptionError {
136    #[error("Encryption key not found: {0}")]
137    KeyNotFound(String),
138
139    #[error("Invalid encryption key: {0}")]
140    InvalidKey(String),
141
142    #[error("Encryption failed: {0}")]
143    EncryptionFailed(String),
144
145    #[error("Decryption failed: {0}")]
146    DecryptionFailed(String),
147
148    #[error("Invalid initialization vector")]
149    InvalidIV,
150
151    #[error("Integrity check failed")]
152    IntegrityCheckFailed,
153
154    #[error("Invalid encrypted data")]
155    InvalidData,
156
157    #[error("Unsupported algorithm: {0}")]
158    UnsupportedAlgorithm(String),
159}
160
161#[derive(Error, Debug)]
162pub enum StorageError {
163    #[error("Storage full: {0}")]
164    Full(String),
165
166    #[error("Storage not available: {0}")]
167    NotAvailable(String),
168
169    #[error("Storage timeout: {0}")]
170    Timeout(String),
171
172    #[error("Storage corruption detected: {0}")]
173    Corruption(String),
174
175    #[error("Storage version mismatch: {0}")]
176    VersionMismatch(String),
177}
178
179// 为错误类型实现一些便利方法
180impl SyncError {
181    pub fn is_retryable(&self) -> bool {
182        match self {
183            SyncError::Network(_)
184            | SyncError::Timeout(_)
185            | SyncError::RateLimitExceeded(_)
186            | SyncError::ResourceExhausted(_) => true,
187            SyncError::Provider(ProviderError::RateLimited(_))
188            | SyncError::Provider(ProviderError::Timeout(_))
189            | SyncError::Provider(ProviderError::ConnectionFailed(_)) => true,
190            _ => false,
191        }
192    }
193
194    pub fn is_fatal(&self) -> bool {
195        match self {
196            SyncError::Provider(ProviderError::NotFound(_))
197            | SyncError::Provider(ProviderError::NotSupported(_))
198            | SyncError::Provider(ProviderError::InvalidCredentials(_))
199            | SyncError::Provider(ProviderError::PermissionDenied(_))
200            | SyncError::Encryption(EncryptionError::KeyNotFound(_))
201            | SyncError::Encryption(EncryptionError::InvalidKey(_))
202            | SyncError::Validation(_) => true,
203            _ => false,
204        }
205    }
206
207    pub fn error_code(&self) -> u32 {
208        match self {
209            SyncError::Config(_) => 1000,
210            SyncError::Provider(_) => 2000,
211            SyncError::Encryption(_) => 3000,
212            SyncError::Storage(_) => 4000,
213            SyncError::Io(_) => 5000,
214            SyncError::Network(_) => 6000,
215            SyncError::Database(_) => 7000,
216            SyncError::Serialization(_) => 9000,
217            SyncError::Validation(_) => 10000,
218            SyncError::Timeout(_) => 11000,
219            SyncError::RateLimitExceeded(_) => 12000,
220            SyncError::AuthenticationFailed(_) => 13000,
221            SyncError::PermissionDenied(_) => 14000,
222            SyncError::FileNotFound(_) => 15000,
223            SyncError::ResourceExhausted(_) => 16000,
224            SyncError::Conflict(_) => 17000,
225            SyncError::IntegrityCheckFailed(_) => 18000,
226            SyncError::RetryLimitExceeded(_) => 19000,
227            SyncError::OperationCanceled => 20000,
228            SyncError::Unsupported(_) => 21000,
229            SyncError::Unknown(_) => 99999,
230        }
231    }
232}
233
234// Result 类型别名
235pub type Result<T> = std::result::Result<T, SyncError>;
236pub type ConfigResult<T> = std::result::Result<T, ConfigError>;
237pub type ProviderResult<T> = std::result::Result<T, ProviderError>;
238pub type EncryptionResult<T> = std::result::Result<T, EncryptionError>;