mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
Adding FinalError, using in crate::Error Display
Helps us add nice and consistently formatted error messages
This commit is contained in:
parent
8707328944
commit
51a88bebba
121
src/error.rs
121
src/error.rs
@ -1,6 +1,9 @@
|
|||||||
use std::{fmt, path::PathBuf};
|
use std::{
|
||||||
|
fmt,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{oof, utils::colors};
|
use crate::{oof, utils::colors::*};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
@ -25,65 +28,111 @@ pub enum Error {
|
|||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
struct FinalError {
|
||||||
|
title: String,
|
||||||
|
details: Vec<String>,
|
||||||
|
hints: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FinalError {
|
||||||
|
pub fn with_title(title: impl ToString) -> Self {
|
||||||
|
Self { title: title.to_string(), details: vec![], hints: vec![] }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn detail(&mut self, detail: impl ToString) -> &mut Self {
|
||||||
|
self.details.push(detail.to_string());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hint(&mut self, hint: impl ToString) -> &mut Self {
|
||||||
|
self.hints.push(hint.to_string());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn display(&self) {
|
||||||
|
// Title
|
||||||
|
eprintln!("{}[ERROR]{} {}", red(), reset(), self.title);
|
||||||
|
|
||||||
|
// Details
|
||||||
|
for detail in &self.details {
|
||||||
|
eprintln!(" {}-{} {}", white(), yellow(), detail);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hints
|
||||||
|
if !self.hints.is_empty() {
|
||||||
|
// Separate by one blank line.
|
||||||
|
eprintln!();
|
||||||
|
for hint in &self.hints {
|
||||||
|
eprintln!("{}hint:{} {}", green(), reset(), hint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure to fix colors
|
||||||
|
eprint!("{}", reset());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Error::MissingExtensionError(filename) => {
|
Error::MissingExtensionError(filename) => {
|
||||||
write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?;
|
FinalError::with_title(format!("Cannot compress to {:?}", filename))
|
||||||
// TODO: show MIME type of the unsupported file
|
.detail("Ouch could not detect the compression format")
|
||||||
write!(f, "cannot compress to {:?}, likely because it has an unsupported (or missing) extension.", filename)
|
.hint("Use a supported format extension, like '.zip' or '.tar.gz'")
|
||||||
|
.hint("Check https://github.com/vrmiguel/ouch for a full list of supported formats")
|
||||||
|
.display();
|
||||||
},
|
},
|
||||||
Error::WalkdirError { reason } => {
|
Error::WalkdirError { reason } => {
|
||||||
write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), reason)
|
FinalError::with_title(reason).display();
|
||||||
},
|
},
|
||||||
Error::FileNotFound(file) => {
|
Error::FileNotFound(file) => {
|
||||||
write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?;
|
if file == Path::new("") {
|
||||||
if file == &PathBuf::from("") {
|
FinalError::with_title("file not found!")
|
||||||
return write!(f, "file not found!");
|
} else {
|
||||||
|
FinalError::with_title(format!("file {:?} not found!", file))
|
||||||
}
|
}
|
||||||
write!(f, "file {:?} not found!", file)
|
.display();
|
||||||
},
|
},
|
||||||
Error::CompressingRootFolder => {
|
Error::CompressingRootFolder => {
|
||||||
write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?;
|
FinalError::with_title("It seems you're trying to compress the root folder.")
|
||||||
let spacing = " ";
|
.detail("This is unadvisable since ouch does compressions in-memory.")
|
||||||
writeln!(f, "It seems you're trying to compress the root folder.")?;
|
.hint("Use a more appropriate tool for this, such as rsync.")
|
||||||
writeln!(
|
.display();
|
||||||
f,
|
|
||||||
"{}This is unadvisable since ouch does compressions in-memory.",
|
|
||||||
spacing
|
|
||||||
)?;
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"{}Use a more appropriate tool for this, such as {}rsync{}.",
|
|
||||||
spacing,
|
|
||||||
colors::green(),
|
|
||||||
colors::reset()
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
Error::MissingArgumentsForCompression => {
|
Error::MissingArgumentsForCompression => {
|
||||||
write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?;
|
FinalError::with_title("Could not compress")
|
||||||
let spacing = " ";
|
.detail("The compress command requires at least 2 arguments")
|
||||||
writeln!(f,"The compress subcommands demands at least 2 arguments, an input file and an output file.")?;
|
.hint("You must provide:")
|
||||||
writeln!(f, "{}Example: `ouch compress img.jpeg img.zip`", spacing)?;
|
.hint(" - At least one input argument.")
|
||||||
write!(f, "{}For more information, run `ouch --help`", spacing)
|
.hint(" - The output argument.")
|
||||||
|
.hint("")
|
||||||
|
.hint("Example: `ouch compress image.png img.zip`")
|
||||||
|
.display();
|
||||||
},
|
},
|
||||||
Error::InternalError => {
|
Error::InternalError => {
|
||||||
write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?;
|
FinalError::with_title("InternalError :(")
|
||||||
write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}https://github.com/vrmiguel/ouch{}", colors::green(), colors::reset())
|
.detail("This should not have happened")
|
||||||
|
.detail("It's probably our fault")
|
||||||
|
.detail("Please help us improve by reporting the issue at:")
|
||||||
|
.detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()))
|
||||||
|
.display();
|
||||||
},
|
},
|
||||||
Error::OofError(err) => {
|
Error::OofError(err) => {
|
||||||
write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), err)
|
FinalError::with_title(err).display();
|
||||||
},
|
},
|
||||||
Error::IoError { reason } => {
|
Error::IoError { reason } => {
|
||||||
write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), reason)
|
FinalError::with_title(reason).display();
|
||||||
},
|
},
|
||||||
Error::CompressionTypo => {
|
Error::CompressionTypo => {
|
||||||
write!(f, "Did you mean {}ouch compress{}?", colors::magenta(), colors::reset())
|
FinalError::with_title("Possible typo detected")
|
||||||
|
.hint(format!("Did you mean '{}ouch compress{}'?", magenta(), reset()))
|
||||||
|
.display();
|
||||||
},
|
},
|
||||||
_err => {
|
_err => {
|
||||||
todo!();
|
todo!();
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user