dioxus_provider/
errors.rs1use thiserror::Error;
53
54#[derive(Error, Debug, Clone, PartialEq)]
56pub enum ProviderError {
57 #[error("Invalid input: {0}")]
59 InvalidInput(String),
60
61 #[error("Network error: {0}")]
63 Network(String),
64
65 #[error("External service '{service}' error: {error}")]
67 ExternalService { service: String, error: String },
68
69 #[error("Data parsing error: {0}")]
71 DataParsing(String),
72
73 #[error("Authentication failed: {0}")]
75 Authentication(String),
76
77 #[error("Authorization failed: {0}")]
79 Authorization(String),
80
81 #[error("Rate limit exceeded: {0}")]
83 RateLimit(String),
84
85 #[error("Operation timed out: {0}")]
87 Timeout(String),
88
89 #[error("Configuration error: {0}")]
91 Configuration(String),
92
93 #[error("Dependency injection failed: {0}")]
95 DependencyInjection(String),
96
97 #[error("Cache error: {0}")]
99 Cache(String),
100
101 #[error("Provider error: {0}")]
103 Generic(String),
104}
105
106#[derive(Error, Debug, Clone, PartialEq)]
108pub enum UserError {
109 #[error("User not found: {id}")]
111 NotFound { id: u32 },
112
113 #[error("User suspended: {reason}")]
115 Suspended { reason: String },
116
117 #[error("User deleted: {id}")]
119 Deleted { id: u32 },
120
121 #[error("Permission denied for user {user_id}: {action}")]
123 PermissionDenied { user_id: u32, action: String },
124
125 #[error("User validation failed: {field}: {reason}")]
127 ValidationFailed { field: String, reason: String },
128
129 #[error("Provider error: {0}")]
131 Provider(#[from] ProviderError),
132}
133
134#[derive(Error, Debug, Clone, PartialEq)]
136pub enum ApiError {
137 #[error("HTTP {status}: {message}")]
139 HttpStatus { status: u16, message: String },
140
141 #[error("JSON parsing failed: {0}")]
143 JsonParsing(String),
144
145 #[error("Request building failed: {0}")]
147 RequestBuilding(String),
148
149 #[error("Response processing failed: {0}")]
151 ResponseProcessing(String),
152
153 #[error("API endpoint not found: {endpoint}")]
155 EndpointNotFound { endpoint: String },
156
157 #[error("API version mismatch: expected {expected}, got {actual}")]
159 VersionMismatch { expected: String, actual: String },
160
161 #[error("Provider error: {0}")]
163 Provider(#[from] ProviderError),
164}
165
166#[derive(Error, Debug, Clone, PartialEq)]
168pub enum DatabaseError {
169 #[error("Database connection failed: {0}")]
171 Connection(String),
172
173 #[error("Query execution failed: {query}: {error}")]
175 QueryExecution { query: String, error: String },
176
177 #[error("Transaction failed: {0}")]
179 Transaction(String),
180
181 #[error("Database migration failed: {0}")]
183 Migration(String),
184
185 #[error("Database constraint violation: {constraint}: {details}")]
187 ConstraintViolation { constraint: String, details: String },
188
189 #[error("Record not found: {table}: {id}")]
191 RecordNotFound { table: String, id: String },
192
193 #[error("Provider error: {0}")]
195 Provider(#[from] ProviderError),
196}
197
198pub type ProviderResult<T> = Result<T, ProviderError>;
200
201pub type UserResult<T> = Result<T, UserError>;
203
204pub type ApiResult<T> = Result<T, ApiError>;
206
207pub type DatabaseResult<T> = Result<T, DatabaseError>;
209
210impl From<String> for ProviderError {
211 fn from(error: String) -> Self {
212 ProviderError::Generic(error)
213 }
214}
215
216impl From<&str> for ProviderError {
217 fn from(error: &str) -> Self {
218 ProviderError::Generic(error.to_string())
219 }
220}
221
222impl From<ProviderError> for String {
223 fn from(error: ProviderError) -> Self {
224 error.to_string()
225 }
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231
232 #[test]
233 fn test_provider_error_display() {
234 let error = ProviderError::InvalidInput("test input".to_string());
235 assert_eq!(error.to_string(), "Invalid input: test input");
236 }
237
238 #[test]
239 fn test_user_error_with_provider_error() {
240 let provider_error = ProviderError::Network("connection failed".to_string());
241 let user_error = UserError::Provider(provider_error);
242 assert_eq!(
243 user_error.to_string(),
244 "Provider error: Network error: connection failed"
245 );
246 }
247
248 #[test]
249 fn test_api_error_http_status() {
250 let error = ApiError::HttpStatus {
251 status: 404,
252 message: "Not Found".to_string(),
253 };
254 assert_eq!(error.to_string(), "HTTP 404: Not Found");
255 }
256
257 #[test]
258 fn test_database_error_constraint_violation() {
259 let error = DatabaseError::ConstraintViolation {
260 constraint: "unique_email".to_string(),
261 details: "Email already exists".to_string(),
262 };
263 assert_eq!(
264 error.to_string(),
265 "Database constraint violation: unique_email: Email already exists"
266 );
267 }
268}