forked from enowdev/enowX-Coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
98 lines (84 loc) · 2.29 KB
/
Copy patherror.rs
File metadata and controls
98 lines (84 loc) · 2.29 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
use serde::Serialize;
use thiserror::Error;
pub type AppResult<T> = Result<T, AppError>;
#[derive(Debug, Error, Serialize)]
#[serde(tag = "kind", content = "message", rename_all = "camelCase")]
pub enum AppError {
#[error("Database error: {0}")]
Database(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("HTTP error: {0}")]
Http(String),
#[error("JSON error: {0}")]
Json(String),
#[error("IO error: {0}")]
Io(String),
#[error("Tauri error: {0}")]
Tauri(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Cancelled")]
Cancelled,
}
impl From<AppError> for String {
fn from(value: AppError) -> Self {
value.to_string()
}
}
impl From<sqlx::Error> for AppError {
fn from(value: sqlx::Error) -> Self {
Self::Database(value.to_string())
}
}
impl From<sqlx::migrate::MigrateError> for AppError {
fn from(value: sqlx::migrate::MigrateError) -> Self {
Self::Database(value.to_string())
}
}
impl From<reqwest::Error> for AppError {
fn from(value: reqwest::Error) -> Self {
Self::Http(value.to_string())
}
}
impl From<serde_json::Error> for AppError {
fn from(value: serde_json::Error) -> Self {
Self::Json(value.to_string())
}
}
impl From<std::io::Error> for AppError {
fn from(value: std::io::Error) -> Self {
Self::Io(value.to_string())
}
}
impl From<tauri::Error> for AppError {
fn from(value: tauri::Error) -> Self {
Self::Tauri(value.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_not_found_error() {
let err = AppError::NotFound("resource".to_string());
assert_eq!(err.to_string(), "Not found: resource");
}
#[test]
fn test_validation_error() {
let err = AppError::Validation("invalid input".to_string());
assert_eq!(err.to_string(), "Validation error: invalid input");
}
#[test]
fn test_cancelled_error() {
let err = AppError::Cancelled;
assert_eq!(err.to_string(), "Cancelled");
}
#[test]
fn test_error_to_string() {
let err: String = AppError::NotFound("test".to_string()).into();
assert_eq!(err, "Not found: test");
}
}