1use std::fmt;
4use std::io;
5use std::path::PathBuf;
6
7#[derive(Debug)]
9pub enum AppError {
10 Database(rusqlite::Error),
11 Io(io::Error),
12 Image(image::ImageError),
13 Exif(String),
14 Ml(String),
15 FileNotFound(PathBuf),
16 DriveNotMounted(PathBuf),
17 Config(String),
18 Cancelled,
19 Other(String),
20}
21
22impl fmt::Display for AppError {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 AppError::Database(e) => write!(f, "Database error: {}", e),
26 AppError::Io(e) => write!(f, "I/O error: {}", e),
27 AppError::Image(e) => write!(f, "Image error: {}", e),
28 AppError::Exif(msg) => write!(f, "EXIF error: {}", msg),
29 AppError::Ml(msg) => write!(f, "ML error: {}", msg),
30 AppError::FileNotFound(path) => write!(f, "File not found: {}", path.display()),
31 AppError::DriveNotMounted(path) => {
32 write!(f, "Drive not mounted: {}", path.display())
33 }
34 AppError::Config(msg) => write!(f, "Configuration error: {}", msg),
35 AppError::Cancelled => write!(f, "Operation cancelled"),
36 AppError::Other(msg) => write!(f, "{}", msg),
37 }
38 }
39}
40
41impl std::error::Error for AppError {}
42
43impl From<rusqlite::Error> for AppError {
44 fn from(e: rusqlite::Error) -> Self {
45 AppError::Database(e)
46 }
47}
48
49impl From<io::Error> for AppError {
50 fn from(e: io::Error) -> Self {
51 AppError::Io(e)
52 }
53}
54
55impl From<image::ImageError> for AppError {
56 fn from(e: image::ImageError) -> Self {
57 AppError::Image(e)
58 }
59}
60
61impl AppError {
62 pub fn recovery_suggestion(&self) -> &str {
63 match self {
64 AppError::Database(_) => {
65 "Try restarting the app. If it persists, database may be corrupted."
66 }
67 AppError::Io(_) => "Check file permissions and available disk space.",
68 AppError::Image(_) => "This image may be corrupted or unsupported.",
69 AppError::Exif(_) => "Metadata could not be read. Photo can still be indexed.",
70 AppError::Ml(_) => "Face detection failed. Try restarting the app.",
71 AppError::FileNotFound(_) => "The file may have been moved or deleted.",
72 AppError::DriveNotMounted(_) => "Reconnect the drive and try again.",
73 AppError::Config(_) => "Reset settings to defaults and try again.",
74 AppError::Cancelled => "",
75 AppError::Other(_) => "Please try again.",
76 }
77 }
78
79 pub fn is_recoverable(&self) -> bool {
80 !matches!(self, AppError::Database(_))
81 }
82}
83
84#[derive(Debug, Clone)]
85pub struct ErrorNotification {
86 pub title: String,
87 pub message: String,
88 pub suggestion: String,
89 pub is_critical: bool,
90}
91
92impl From<&AppError> for ErrorNotification {
93 fn from(error: &AppError) -> Self {
94 let title = match error {
95 AppError::Database(_) => "Database Error",
96 AppError::Io(_) => "File Error",
97 AppError::Image(_) => "Image Error",
98 AppError::Exif(_) => "Metadata Error",
99 AppError::Ml(_) => "Face Detection Error",
100 AppError::FileNotFound(_) => "File Not Found",
101 AppError::DriveNotMounted(_) => "Drive Not Connected",
102 AppError::Config(_) => "Settings Error",
103 AppError::Cancelled => "Cancelled",
104 AppError::Other(_) => "Error",
105 };
106
107 Self {
108 title: title.to_string(),
109 message: error.to_string(),
110 suggestion: error.recovery_suggestion().to_string(),
111 is_critical: !error.is_recoverable(),
112 }
113 }
114}