1use std::path::PathBuf;
2
3use globset::Error as GlobSetError;
4
5#[derive(Debug)]
10pub enum DatabaseError {
11 FileNotFound,
13 IOError(std::io::Error),
15 InvalidGlobSet(GlobSetError),
17 FileTooLarge(PathBuf, usize, usize),
19 ChangeLogInUse,
23 PoisonedLogMutex,
28 WatcherInit(notify::Error),
30 WatcherWatch(notify::Error),
32 WatcherNotActive,
34}
35
36impl std::fmt::Display for DatabaseError {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 match self {
39 Self::FileNotFound => write!(f, "file not found in database"),
40 Self::IOError(err) => write!(f, "I/O error: {err}"),
41 Self::InvalidGlobSet(err) => write!(f, "failed to build exclusion filter from patterns: {err}"),
42 Self::FileTooLarge(path, size, max_size) => {
43 write!(
44 f,
45 "file at {} is too large to be processed: {} bytes (maximum is {} bytes)",
46 path.display(),
47 size,
48 max_size
49 )
50 }
51 Self::ChangeLogInUse => {
52 write!(f, "cannot commit changelog because it is still in use by another thread")
53 }
54 Self::PoisonedLogMutex => {
55 write!(f, "changelog is in an unrecoverable state because a thread panicked while modifying it")
56 }
57 Self::WatcherInit(err) => write!(f, "failed to initialize file watcher: {err}"),
58 Self::WatcherWatch(err) => write!(f, "failed to watch path: {err}"),
59 Self::WatcherNotActive => write!(f, "watcher is not currently watching - call watch() first"),
60 }
61 }
62}
63
64impl std::error::Error for DatabaseError {
65 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
66 match self {
67 Self::IOError(err) => Some(err),
68 Self::InvalidGlobSet(err) => Some(err),
69 Self::WatcherInit(err) | Self::WatcherWatch(err) => Some(err),
70 _ => None,
71 }
72 }
73}
74
75impl From<std::io::Error> for DatabaseError {
76 fn from(error: std::io::Error) -> Self {
77 Self::IOError(error)
78 }
79}
80
81impl From<GlobSetError> for DatabaseError {
82 fn from(error: GlobSetError) -> Self {
83 Self::InvalidGlobSet(error)
84 }
85}