-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
168 lines (127 loc) · 4.67 KB
/
error.rs
File metadata and controls
168 lines (127 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::{io, path::PathBuf};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum FileSyncError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("Configuration error: {0}")]
Config(String),
#[error("Authentication error: {0}")]
Auth(String),
#[error("Plugin error: {0}")]
Plugin(String),
#[error("Synchronization error: {0}")]
Sync(String),
#[error("Safety error: {0}")]
Safety(String),
#[error("Performance error: {0}")]
Performance(String),
#[error("Logging error: {0}")]
Log(String),
#[error("File not found: {0}")]
FileNotFound(PathBuf),
#[error("File corrupted: {0}")]
FileCorrupted(PathBuf),
#[error("Network error: {0}")]
Network(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Operation cancelled: {0}")]
Cancelled(String),
#[error("Service error: {0}")]
Service(String),
#[error("TUI error: {0}")]
Tui(String),
#[error("Plugin not found: {0}")]
PluginNotFound(String),
#[error("Invalid credentials: {0}")]
InvalidCredentials(String),
#[error("Rate limit exceeded: {0}")]
RateLimit(String),
#[error("Quota exceeded: {0}")]
QuotaExceeded(String),
#[error("Unknown error: {0}")]
Unknown(String),
}
impl From<toml::de::Error> for FileSyncError {
fn from(err: toml::de::Error) -> Self {
FileSyncError::Config(format!("Error reading configuration: {}", err))
}
}
impl From<toml::ser::Error> for FileSyncError {
fn from(err: toml::ser::Error) -> Self {
FileSyncError::Config(format!("Error saving configuration: {}", err))
}
}
impl From<serde_json::Error> for FileSyncError {
fn from(err: serde_json::Error) -> Self {
FileSyncError::Serialization(format!("JSON error: {}", err))
}
}
impl From<reqwest::Error> for FileSyncError {
fn from(err: reqwest::Error) -> Self {
FileSyncError::Network(format!("HTTP request error: {}", err))
}
}
impl From<oauth2::basic::BasicRequestTokenError> for FileSyncError {
fn from(err: oauth2::basic::BasicRequestTokenError) -> Self {
FileSyncError::Auth(format!("OAuth2 authentication error: {}", err))
}
}
impl From<notify::Error> for FileSyncError {
fn from(err: notify::Error) -> Self {
FileSyncError::Io(io::Error::new(io::ErrorKind::Other, err))
}
}
impl From<log::SetLoggerError> for FileSyncError {
fn from(err: log::SetLoggerError) -> Self {
FileSyncError::Log(format!("Error configuring logger: {}", err))
}
}
impl From<crossterm::ErrorKind> for FileSyncError {
fn from(err: crossterm::ErrorKind) -> Self {
FileSyncError::Tui(format!("Terminal UI error: {}", err))
}
}
impl From<daemonize::DaemonizeError> for FileSyncError {
fn from(err: daemonize::DaemonizeError) -> Self {
FileSyncError::Service(format!("Service error: {}", err))
}
}
pub type Result<T> = std::result::Result<T, FileSyncError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_conversion() {
// Test IO error conversion
let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let sync_err: FileSyncError = io_err.into();
assert!(matches!(sync_err, FileSyncError::Io(_)));
// Test configuration error
let config_err = FileSyncError::Config("invalid configuration".to_string());
assert!(matches!(config_err, FileSyncError::Config(_)));
// Test authentication error
let auth_err = FileSyncError::Auth("invalid token".to_string());
assert!(matches!(auth_err, FileSyncError::Auth(_)));
// Test plugin error
let plugin_err = FileSyncError::PluginNotFound("google_drive".to_string());
assert!(matches!(plugin_err, FileSyncError::PluginNotFound(_)));
// Test service error
let service_err = FileSyncError::Service("failed to start".to_string());
assert!(matches!(service_err, FileSyncError::Service(_)));
}
#[test]
fn test_error_display() {
let err = FileSyncError::FileNotFound(PathBuf::from("test.txt"));
assert_eq!(err.to_string(), "File not found: test.txt");
let err = FileSyncError::Network("connection lost".to_string());
assert_eq!(err.to_string(), "Network error: connection lost");
let err = FileSyncError::RateLimit("too many requests".to_string());
assert_eq!(err.to_string(), "Rate limit exceeded: too many requests");
let err = FileSyncError::QuotaExceeded("storage limit reached".to_string());
assert_eq!(err.to_string(), "Quota exceeded: storage limit reached");
}
}