1 //! Error types definitions.
3 //! All usage errors will pass throught the Error enum, a lot of them in the Error::Custom.
10 use crate::{accessible::is_running_in_accessible_mode, utils::colors::*};
12 /// All errors that can be generated by `ouch`
15 /// Not every IoError, some of them get filtered by `From<io::Error>` into other variants
16 IoError { reason: String },
17 /// From lzzzz::lz4f::Error
18 Lz4Error { reason: String },
19 /// Detected from io::Error if .kind() is io::ErrorKind::NotFound
20 NotFound { error_title: String },
21 /// NEEDS MORE CONTEXT
22 AlreadyExists { error_title: String },
23 /// From zip::result::ZipError::InvalidArchive
24 InvalidZipArchive(&'static str),
25 /// Detected from io::Error if .kind() is io::ErrorKind::PermissionDenied
26 PermissionDenied { error_title: String },
27 /// From zip::result::ZipError::UnsupportedArchive
28 UnsupportedZipArchive(&'static str),
30 CompressingRootFolder,
31 /// Specialized walkdir's io::Error wrapper with additional information on the error
32 WalkdirError { reason: String },
33 /// Custom and unique errors are reported in this variant
34 Custom { reason: FinalError },
37 /// Alias to std's Result with ouch's Error
38 pub type Result<T> = std::result::Result<T, Error>;
40 /// A string either heap-allocated or located in static storage
41 pub type CowStr = Cow<'static, str>;
43 /// Pretty final error message for end users, crashing the program after display.
44 #[derive(Clone, Debug, Default, PartialEq, Eq)]
45 pub struct FinalError {
46 /// Should be made of just one line, appears after the "\[ERROR\]" part
48 /// Shown as a unnumbered list in yellow
50 /// Shown as green at the end to give hints on how to work around this error, if it's fixable
54 impl Display for FinalError {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 // When in ACCESSIBLE mode, the square brackets are suppressed
59 if is_running_in_accessible_mode() {
60 write!(f, "{}ERROR{}: {}", *RED, *RESET, self.title)?;
62 write!(f, "{}[ERROR]{} {}", *RED, *RESET, self.title)?;
66 for detail in &self.details {
67 write!(f, "\n - {}{}{}", *YELLOW, detail, *RESET)?;
71 if !self.hints.is_empty() {
72 // Separate by one blank line.
74 // to reduce redundant output for text-to-speach systems, braille
75 // displays and so on, only print "hints" once in ACCESSIBLE mode
76 if is_running_in_accessible_mode() {
77 write!(f, "\n{}hints:{}", *GREEN, *RESET)?;
78 for hint in &self.hints {
79 write!(f, "\n{hint}")?;
82 for hint in &self.hints {
83 write!(f, "\n{}hint:{} {}", *GREEN, *RESET, hint)?;
95 pub fn with_title(title: impl Into<CowStr>) -> Self {
103 /// Add one detail line, can have multiple
105 pub fn detail(mut self, detail: impl Into<CowStr>) -> Self {
106 self.details.push(detail.into());
110 /// Add one hint line, can have multiple
112 pub fn hint(mut self, hint: impl Into<CowStr>) -> Self {
113 self.hints.push(hint.into());
118 impl fmt::Display for Error {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120 let err = match self {
121 Error::WalkdirError { reason } => FinalError::with_title(reason.to_string()),
122 Error::NotFound { error_title } => FinalError::with_title(error_title.to_string()).detail("File not found"),
123 Error::CompressingRootFolder => {
124 FinalError::with_title("It seems you're trying to compress the root folder.")
125 .detail("This is unadvisable since ouch does compressions in-memory.")
126 .hint("Use a more appropriate tool for this, such as rsync.")
128 Error::IoError { reason } => FinalError::with_title(reason.to_string()),
129 Error::Lz4Error { reason } => FinalError::with_title(reason.to_string()),
130 Error::AlreadyExists { error_title } => {
131 FinalError::with_title(error_title.to_string()).detail("File already exists")
133 Error::InvalidZipArchive(reason) => FinalError::with_title("Invalid zip archive").detail(*reason),
134 Error::PermissionDenied { error_title } => {
135 FinalError::with_title(error_title.to_string()).detail("Permission denied")
137 Error::UnsupportedZipArchive(reason) => FinalError::with_title("Unsupported zip archive").detail(*reason),
138 Error::Custom { reason } => reason.clone(),
145 impl From<std::io::Error> for Error {
146 fn from(err: std::io::Error) -> Self {
148 std::io::ErrorKind::NotFound => Self::NotFound {
149 error_title: err.to_string(),
151 std::io::ErrorKind::PermissionDenied => Self::PermissionDenied {
152 error_title: err.to_string(),
154 std::io::ErrorKind::AlreadyExists => Self::AlreadyExists {
155 error_title: err.to_string(),
157 _other => Self::IoError {
158 reason: err.to_string(),
164 impl From<lzzzz::lz4f::Error> for Error {
165 fn from(err: lzzzz::lz4f::Error) -> Self {
167 reason: err.to_string(),
172 impl From<zip::result::ZipError> for Error {
173 fn from(err: zip::result::ZipError) -> Self {
174 use zip::result::ZipError;
176 ZipError::Io(io_err) => Self::from(io_err),
177 ZipError::InvalidArchive(filename) => Self::InvalidZipArchive(filename),
178 ZipError::FileNotFound => Self::Custom {
179 reason: FinalError::with_title("Unexpected error in zip archive").detail("File not found"),
181 ZipError::UnsupportedArchive(filename) => Self::UnsupportedZipArchive(filename),
186 impl From<ignore::Error> for Error {
187 fn from(err: ignore::Error) -> Self {
189 reason: err.to_string(),
194 impl From<FinalError> for Error {
195 fn from(err: FinalError) -> Self {
196 Self::Custom { reason: err }