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}
29
30impl std::fmt::Display for DatabaseError {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 match self {
33 Self::FileNotFound => write!(f, "file not found in database"),
34 Self::IOError(err) => write!(f, "I/O error: {err}"),
35 Self::InvalidGlobSet(err) => write!(f, "failed to build exclusion filter from patterns: {err}"),
36 Self::FileTooLarge(path, size, max_size) => {
37 write!(
38 f,
39 "file at {} is too large to be processed: {} bytes (maximum is {} bytes)",
40 path.display(),
41 size,
42 max_size
43 )
44 }
45 Self::ChangeLogInUse => {
46 write!(f, "cannot commit changelog because it is still in use by another thread")
47 }
48 Self::PoisonedLogMutex => {
49 write!(f, "changelog is in an unrecoverable state because a thread panicked while modifying it")
50 }
51 }
52 }
53}
54
55impl std::error::Error for DatabaseError {
56 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57 match self {
58 Self::IOError(err) => Some(err),
59 Self::InvalidGlobSet(err) => Some(err),
60 _ => None,
61 }
62 }
63}
64
65impl From<std::io::Error> for DatabaseError {
66 fn from(error: std::io::Error) -> Self {
67 Self::IOError(error)
68 }
69}
70
71impl From<GlobSetError> for DatabaseError {
72 fn from(error: GlobSetError) -> Self {
73 Self::InvalidGlobSet(error)
74 }
75}