1 //! Error types definitions.
3 //! All usage errors will pass through 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 },
35 /// Invalid format passed to `--format`
36 InvalidFormat { reason: String },
37 /// From sevenz_rust::Error
38 SevenzipError(sevenz_rust::Error),
41 /// Alias to std's Result with ouch's Error
42 pub type Result<T> = std::result::Result<T, Error>;
44 /// A string either heap-allocated or located in static storage
45 pub type CowStr = Cow<'static, str>;
47 /// Pretty final error message for end users, crashing the program after display.
48 #[derive(Clone, Debug, Default, PartialEq, Eq)]
49 pub struct FinalError {
50 /// Should be made of just one line, appears after the "\[ERROR\]" part
52 /// Shown as a unnumbered list in yellow
54 /// Shown as green at the end to give hints on how to work around this error, if it's fixable
58 impl Display for FinalError {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 // When in ACCESSIBLE mode, the square brackets are suppressed
63 if is_running_in_accessible_mode() {
64 write!(f, "{}ERROR{}: {}", *RED, *RESET, self.title)?;
66 write!(f, "{}[ERROR]{} {}", *RED, *RESET, self.title)?;
70 for detail in &self.details {
71 write!(f, "\n - {}{}{}", *YELLOW, detail, *RESET)?;
75 if !self.hints.is_empty() {
76 // Separate by one blank line.
78 // to reduce redundant output for text-to-speech systems, braille
79 // displays and so on, only print "hints" once in ACCESSIBLE mode
80 if is_running_in_accessible_mode() {
81 write!(f, "\n{}hints:{}", *GREEN, *RESET)?;
82 for hint in &self.hints {
83 write!(f, "\n{hint}")?;
86 for hint in &self.hints {
87 write!(f, "\n{}hint:{} {}", *GREEN, *RESET, hint)?;
99 pub fn with_title(title: impl Into<CowStr>) -> Self {
107 /// Add one detail line, can have multiple
109 pub fn detail(mut self, detail: impl Into<CowStr>) -> Self {
110 self.details.push(detail.into());
114 /// Add one hint line, can have multiple
116 pub fn hint(mut self, hint: impl Into<CowStr>) -> Self {
117 self.hints.push(hint.into());
122 impl fmt::Display for Error {
123 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
124 let err = match self {
125 Error::WalkdirError { reason } => FinalError::with_title(reason.to_string()),
126 Error::NotFound { error_title } => FinalError::with_title(error_title.to_string()).detail("File not found"),
127 Error::CompressingRootFolder => {
128 FinalError::with_title("It seems you're trying to compress the root folder.")
129 .detail("This is unadvisable since ouch does compressions in-memory.")
130 .hint("Use a more appropriate tool for this, such as rsync.")
132 Error::IoError { reason } => FinalError::with_title(reason.to_string()),
133 Error::Lz4Error { reason } => FinalError::with_title(reason.to_string()),
134 Error::AlreadyExists { error_title } => {
135 FinalError::with_title(error_title.to_string()).detail("File already exists")
137 Error::InvalidZipArchive(reason) => FinalError::with_title("Invalid zip archive").detail(*reason),
138 Error::PermissionDenied { error_title } => {
139 FinalError::with_title(error_title.to_string()).detail("Permission denied")
141 Error::UnsupportedZipArchive(reason) => FinalError::with_title("Unsupported zip archive").detail(*reason),
142 Error::InvalidFormat { reason } => FinalError::with_title("Invalid archive format").detail(reason.clone()),
143 Error::Custom { reason } => reason.clone(),
144 Error::SevenzipError(reason) => FinalError::with_title("7z error").detail(reason.to_string()),
151 impl From<std::io::Error> for Error {
152 fn from(err: std::io::Error) -> Self {
154 std::io::ErrorKind::NotFound => Self::NotFound {
155 error_title: err.to_string(),
157 std::io::ErrorKind::PermissionDenied => Self::PermissionDenied {
158 error_title: err.to_string(),
160 std::io::ErrorKind::AlreadyExists => Self::AlreadyExists {
161 error_title: err.to_string(),
163 _other => Self::IoError {
164 reason: err.to_string(),
170 impl From<zip::result::ZipError> for Error {
171 fn from(err: zip::result::ZipError) -> Self {
172 use zip::result::ZipError;
174 ZipError::Io(io_err) => Self::from(io_err),
175 ZipError::InvalidArchive(filename) => Self::InvalidZipArchive(filename),
176 ZipError::FileNotFound => Self::Custom {
177 reason: FinalError::with_title("Unexpected error in zip archive").detail("File not found"),
179 ZipError::UnsupportedArchive(filename) => Self::UnsupportedZipArchive(filename),
184 impl From<unrar::error::UnrarError> for Error {
185 fn from(err: unrar::error::UnrarError) -> Self {
187 reason: FinalError::with_title("Unexpected error in rar archive").detail(format!("{:?}", err.code)),
192 impl From<sevenz_rust::Error> for Error {
193 fn from(err: sevenz_rust::Error) -> Self {
194 Self::SevenzipError(err)
198 impl From<ignore::Error> for Error {
199 fn from(err: ignore::Error) -> Self {
201 reason: err.to_string(),
206 impl From<FinalError> for Error {
207 fn from(err: FinalError) -> Self {
208 Self::Custom { reason: err }