xvc_file/
error.rs

1//! [Error] codes and messages for xvc-file crate
2use log::{debug, error, info, trace, warn};
3use xvc_core::{XvcConfigError, XvcEcsError, XvcWalkerError};
4
5use std::fmt::Debug;
6use std::io;
7use std::path::PathBuf;
8use std::time::SystemTimeError;
9use thiserror::Error as ThisError;
10
11/// Error messages for xvc-file
12#[allow(missing_docs)]
13#[derive(ThisError, Debug)]
14pub enum Error {
15    #[error("Sorry. {0} is not implemented yet")]
16    Todo(&'static str),
17    #[error("{source}")]
18    AnyhowError {
19        #[from]
20        source: anyhow::Error,
21    },
22    #[error("Cannot find {xvc_path} in cache: {cache_path}")]
23    CannotFindFileInCache {
24        xvc_path: String,
25        cache_path: String,
26    },
27    #[error("File not found: {path}")]
28    FileNotFound { path: PathBuf },
29    #[error("Internal Error: {message}")]
30    InternalError { message: String },
31    #[error("Walker Error: {source}")]
32    WalkerError {
33        #[from]
34        source: XvcWalkerError,
35    },
36    #[error("Ecs Error: {source}")]
37    EcsError {
38        #[from]
39        source: XvcEcsError,
40    },
41    #[error("Storage Error: {source}")]
42    StorageError {
43        #[from]
44        source: xvc_storage::error::Error,
45    },
46    #[error("Target is ignored, please unignore in .xvcignore: {path}")]
47    TargetIgnored { path: String },
48
49    #[error("[E2004] Requires xvc repository.")]
50    RequiresXvcRepository,
51
52    #[error("Xvc Core Error: {source}")]
53    XvcCoreError {
54        #[from]
55        source: xvc_core::error::Error,
56    },
57    #[error("Xvc Config Error: {source}")]
58    XvcConfigError {
59        #[from]
60        source: XvcConfigError,
61    },
62    #[error("I/O Error: {source}")]
63    IoError {
64        #[from]
65        source: io::Error,
66    },
67    #[error("Enum Parsing Error")]
68    StrumError {
69        #[from]
70        source: strum::ParseError,
71    },
72    #[error("Crossbeam Send Error for Type: {t:?} {cause:?}")]
73    CrossbeamSendError { t: String, cause: String },
74
75    #[error("Strip Prefix Error")]
76    StripPrefixError {
77        #[from]
78        source: std::path::StripPrefixError,
79    },
80    #[error("Relative Path Strip Prefix Error: {:?}", e)]
81    RelativeStripPrefixError { e: relative_path::StripPrefixError },
82
83    #[error("System time error")]
84    SystemTimeError {
85        #[from]
86        source: SystemTimeError,
87    },
88    #[error("Xvc does not support content digest for symlink: {path}")]
89    ContentDigestNotSupported { path: PathBuf },
90
91    #[error("Poisoned Locks: {t} {cause}")]
92    LockPoisonError { t: String, cause: String },
93
94    #[error("Multiple files found to share")]
95    MultipleFilesToShare,
96
97    #[error("No files found to share")]
98    NoFilesToShare,
99
100    #[error("Error parsing the duration")]
101    DurationError {
102        #[from]
103        source: humantime::DurationError,
104    },
105
106    #[error("{message}: {files}")]
107    SourcesHaveChanged { message: String, files: String },
108}
109
110impl<T> From<crossbeam_channel::SendError<T>> for Error
111where
112    T: Debug,
113{
114    fn from(e: crossbeam_channel::SendError<T>) -> Self {
115        Error::CrossbeamSendError {
116            t: format!("{:#?}", e.0),
117            cause: e.to_string(),
118        }
119    }
120}
121
122impl<T> From<std::sync::PoisonError<T>> for Error
123where
124    T: std::fmt::Debug,
125{
126    fn from(e: std::sync::PoisonError<T>) -> Self {
127        Error::LockPoisonError {
128            t: format!("{:#?}", e),
129            cause: e.to_string(),
130        }
131    }
132}
133
134impl Error {
135    /// Write error message to stderr using [log::debug] and return the error
136    pub fn debug(self) -> Self {
137        debug!("{}", self);
138        self
139    }
140    /// Write error message to stderr using [log::trace] and return the error
141    pub fn trace(self) -> Self {
142        trace!("{}", self);
143        self
144    }
145
146    /// Write error message to stderr using [log::warn] and return the error
147    pub fn warn(self) -> Self {
148        warn!("{}", self);
149        self
150    }
151    /// Write error message to stderr using [log::error] and return the error
152    pub fn error(self) -> Self {
153        error!("{}", self);
154        self
155    }
156    /// Write error message to stderr using [log::info] and return the error
157    pub fn info(self) -> Self {
158        info!("{}", self);
159        self
160    }
161    /// Write error message to stderr using [panic!] and quit.
162    pub fn panic(self) -> Self {
163        panic!("{}", self);
164    }
165}
166
167/// Result type for xvc-file crate
168pub type Result<T> = std::result::Result<T, Error>;